在Linux下编译C代码
我正在尝试在Linux中编译一个C代码,以便在我的Raspberry Pi上制作软盘音乐。我使用Scott Vincent的代码来控制GPIO引脚,但我无法编译它。我正在终端中使用此命令:在Linux下编译C代码,c,linux,raspberry-pi,gpio,C,Linux,Raspberry Pi,Gpio,我正在尝试在Linux中编译一个C代码,以便在我的Raspberry Pi上制作软盘音乐。我使用Scott Vincent的代码来控制GPIO引脚,但我无法编译它。我正在终端中使用此命令: gcc -o floppy_music floppy_music.c 代码如下: # -------------------------------------- # Written by Scott Vincent # 16 Feb 2014 # -----------------------------
gcc -o floppy_music floppy_music.c
代码如下:
# --------------------------------------
# Written by Scott Vincent
# 16 Feb 2014
# --------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
// pin 11 = wiringPi Pin 0. Use this for motor direction.
const int dirPin = 0;
// pin 12 supports pwm mode but it turns out I didn't need pwm mode in the end!
// pin 12 = wiringPi Pin 1. Use this for stepper motor.
const int stepPin = 1;
// Define an octave with naturals and sharps (Zz = rest)
enum { Cn, Cs, Dn, Ds, En, Fn, Fs, Gn, Gs, An, As, Bn, Zz };
// Define another one with flats and remaining sharps
enum { Bs, Df, Dn2, Ef, En2, Es, Gf, Gn2, Af, An2, Bf, Bn2, Zz2 };
/**
* Frequencies in hundredths of Hz, e.g. middle A = 44000
* 4 Octaves with 12 notes per octave, i.e. C to B
*/
const int freq[4][12] = {
{ 13081,13859,14683,15556,16481,17461,18500,19600,20765,22000,23308,24694 },
{ 26163,27718,29366,31113,32963,34923,36999,39200,41530,44000,46616,49388 },
{ 52325,55437,58733,62225,65925,69846,73999,78399,83061,88000,93233,98777 },
{ 104650,110873,117466,124451,131851,139691,147998,156798,166122,176000,186466,197553 }
};
/**
* Frequency (in Hz) is converted to Floppy Delay using the formula:
* 314000 / frequency = floppy delay
* so middle A = 314000 / 440 = 714
*
* Lowest realistic note is delay = 1550
* Highest realistic note is delay = 210
*/
const int floppyConv = 31400000;
// Calculate all our floppy delays at the start
int floppyDelay[4][12];
// Song1 is the C major scale (note, octave, length)
const int song1_tempo = 120;
const int song1[][3] = {
{ Cn, 1, 1 },
{ Dn, 1, 1 },
{ En, 1, 1 },
{ Fn, 1, 1 },
{ Gn, 1, 1 },
{ An, 1, 1 },
{ Bn, 1, 1 },
{ Cn, 2, 1 },
{ -1, -1, -1 }
};
// Song2 is The Imperial March from Star Wars (note, octave, length)
const int song2_tempo = 104 * 8;
const int song2[][3] = {
{ Gn, 1, 8 }, // Bar 1
{ Gn, 1, 8 },
{ Gn, 1, 8 },
{ Ef, 1, 6 },
{ Bf, 1, 2 },
{ Gn, 1, 8 },
{ Ef, 1, 6 },
{ Bf, 1, 2 },
{ Gn, 1, 16 },
{ Dn, 2, 8 },
{ Dn, 2, 8 },
{ Dn, 2, 8 },
{ Ef, 2, 6 },
{ Bf, 1, 2 },
{ Gf, 1, 8 }, // Bar 4
{ Ef, 1, 6 },
{ Bf, 1, 2 },
{ Gn, 1, 16 },
{ Gn, 2, 8 },
{ Gn, 1, 6 },
{ Gn, 1, 2 },
{ Gn, 2, 8 },
{ Gf, 2, 6 },
{ Fn, 2, 2 },
{ En, 2, 2 },
{ Ds, 2, 2 },
{ En, 2, 4 },
{ Zz, 0, 4 },
{ Gs, 1, 4 },
{ Cs, 2, 8 },
{ Bs, 2, 6 },
{ Bn, 1, 2 },
{ Bf, 1, 2 }, // Bar 7
{ An, 1, 2 },
{ Bf, 1, 4 },
{ Zz, 0, 4 },
{ Ef, 1, 4 },
{ Gf, 1, 8 },
{ Ef, 1, 6 },
{ Gf, 1, 2 },
{ Bf, 1, 8 },
{ Gn, 1, 6 },
{ Bf, 1, 2 },
{ Dn, 2, 16 },
{ Gn, 2, 8 },
{ Gn, 1, 6 },
{ Gn, 1, 2 },
{ Gn, 2, 8 },
{ Gf, 2, 6 },
{ Fn, 2, 2 },
{ En, 2, 2 }, // Bar 10
{ Ds, 2, 2 },
{ En, 2, 4 },
{ Zz, 0, 4 },
{ Gs, 1, 4 },
{ Cs, 2, 8 },
{ Bs, 2, 6 },
{ Bn, 1, 2 },
{ Bf, 1, 2 },
{ An, 1, 2 },
{ Bf, 1, 4 },
{ Zz, 0, 4 },
{ Ef, 1, 4 },
{ Gf, 1, 8 },
{ Ef, 1, 6 },
{ Bf, 1, 2 },
{ Gn, 1, 8 },
{ Ef, 1, 6 },
{ Bf, 1, 2 },
{ Gn, 1, 16 },
{ -1, -1, -1 }
};
/**
*
*/
static void resetMotor()
{
// To reset head position move back 10 then forward 5
digitalWrite(dirPin, LOW);
for (int i=0; i < 10; i++){
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(1);
}
digitalWrite(dirPin, HIGH);
for (int i=0; i < 5; i++){
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(1);
}
delay(400);
}
/**
*
*/
static int init()
{
if (wiringPiSetup() == -1){
printf("Failed to initialize wiringPi\n");
return 1;
}
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
resetMotor();
for (int octave = 0; octave < 4; octave++){
for (int note = 0; note < 12; note++){
floppyDelay[octave][note] = floppyConv / freq[octave][note];
}
}
return 0;
}
/**
*
*/
static void playNote(int note, int octave, int length)
{
static int dir = 1;
int pause = floppyDelay[octave][note] * 10;
int endTime = millis() + length;
while (millis() < endTime){
digitalWrite(dirPin, dir);
if (dir == 0)
dir = 1;
else
dir = 0;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delayMicroseconds(pause);
}
}
/**
*
*/
static void rest(int length)
{
int endTime = millis() + length;
while (millis() < endTime){
delay(5);
}
}
/**
* song[note_num][note, octave, length]
*/
static void playSong(const int song[][3], const int tempo)
{
// Convert tempo in BPM to millisecs
int noteLen = 60000 / tempo;
for (int i = 0; song[i][0] != -1; i++){
int length = song[i][2] * noteLen;
if (song[i][0] == Zz){
rest(length);
}
else {
playNote(song[i][0], song[i][1], (7 * length) / 8);
rest(length / 8);
}
}
}
/**
*
*/
int main()
{
if (init() != 0){
printf("init failed - Exiting\n");
return 1;
}
playSong(song2, song2_tempo);
return 0;
}
但有趣的是,这段代码对他有效,那么有人知道问题出在哪里吗?这段代码对你的朋友有效,因为他的编译器的默认设置碰巧忽略了无效的预处理器指令而没有失败,并且还接受所有其他代码(在C99中有效) 就预处理内容而言:
# --------------------------------------
# Written by Scott Vincent
# 16 Feb 2014
# --------------------------------------
但这些不是评论。这应该是(或您最喜欢的/*…*/
样式):
或使用C++风格的注释(也适用于C99):
以#
开头的行是预处理器指令;用于在编译之前处理文件的预处理器的特殊命令(例如#if
)。只有一组特定的有效命令。这些不是评论
您可以添加编译器标志以使gcc
接受这一点,但最好对注释使用正确的注释语法,并将#
留给实际的预处理指令
对于其余问题,您可以按照编译器的建议执行,并“使用选项
-std=99
或-std=gnu99
编译代码”。例如,在中为循环声明变量(为(int n…
)至少需要C99。您知道C中的注释不是以#
开头的吗?对于其余部分,只需阅读错误消息,所有错误消息都非常直截了当,有些还建议您应该如何解决它们。使用选项-std=99或-std=gnu99用gcc-std=gnu99-Wall-g
编译您的codeCompile有很多问题,但是我应该如何向命令中添加选项?我应该保留或替换-o吗?@danrodi如果您查看,您将看到-o
指定了输出文件名。您不想替换该文件名。您可能需要在Linux shell中进行一些实验,并更加熟悉命令的概念ne参数;它将在许多方面帮助您,不仅仅是解决这个问题,而且值得您花费时间。好的,但现在它给了我一堆未定义的引用错误。@danrodi这些是独立的问题。我建议您也花一点时间学习C编译器和链接器的工作原理。请阅读并从那里开始。请原谅se不要编辑原始问题以包含新的“未定义引用”错误。请先尝试自己解决这些问题,然后如果您尝试了某个问题并且遇到了问题,请提出新的单独问题。
# --------------------------------------
# Written by Scott Vincent
# 16 Feb 2014
# --------------------------------------
/*
--------------------------------------
Written by Scott Vincent
16 Feb 2014
--------------------------------------
*/
// --------------------------------------
// Written by Scott Vincent
// 16 Feb 2014
// --------------------------------------