继续得到隐式声明错误

编译时我不断收到这些错误。 我修改了在arduino上运行的代码,以便在我的覆盆子pi上运行。

test1.c: In function 'loop': test1.c:24:3: warning: implicit declaration of function 'rotateDeg' [-Wimplicit-function-declaration] test1.c:33:3: warning: implicit declaration of function 'rotate' [-Wimplicit-function-declaration] test1.c: At top level: test1.c:42:6: warning: conflicting types for 'rotate' [enabled by default] test1.c:33:3: note: previous implicit declaration of 'rotate' was here test1.c: In function 'rotate': test1.c:46:3: warning: implicit declaration of function 'abs' [-Wimplicit-function-declaration] test1.c: At top level: test1.c:61:6: warning: conflicting types for 'rotateDeg' [enabled by default] test1.c:24:3: note: previous implicit declaration of 'rotateDeg' was here /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o: In function `_start': (.text+0x34): undefined reference to `main' collect2: ld returned 1 exit status 

这是我的源代码:

 #include  #include  #include  #define DIR_PIN 0 #define STEP_PIN 3 void setup() { pinMode(DIR_PIN, OUTPUT); pinMode(STEP_PIN, OUTPUT); } void loop(){ rotateDeg(360, 1); delay(1000); rotateDeg(-360, .1); //reverse delay(1000); rotate(1600, .5); delay(1000); rotate(-1600, .25); //reverse delay(1000); } void rotate(int steps, float speed){ //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement) //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger int dir = (steps > 0)? HIGH:LOW; steps = abs(steps); digitalWrite(DIR_PIN,dir); float usDelay = (1/speed) * 70; for(int i=0; i  1 with 1 being fastest - Slower is stronger int dir = (deg > 0)? HIGH:LOW; digitalWrite(DIR_PIN,dir); int steps = abs(deg)*(1/0.225); float usDelay = (1/speed) * 70; for(int i=0; i < steps; i++){ digitalWrite(STEP_PIN, HIGH); delayMicroseconds(usDelay); digitalWrite(STEP_PIN, LOW); delayMicroseconds(usDelay); } } 

当存在隐式声明的函数时,您会收到隐式声明警告。

隐式声明的函数是一个既没有原型也没有定义的函数,这就是编译器无法validation您想要对函数做什么的原因。

如果没有先前的函数声明可用,那么它的第一个实例被假定为隐式返回类型为int的声明,并且没有假设参数。

只需保留函数rotaterotatedeg的声明,如下所示:

 void rotate (int , float ); 

 void rotateDeg (float , float ); 

在循环中使用它之前:

 void loop(){ rotateDeg(360, 1); .... .... rotate(1600, .5); ... rotate(-1600, .25); //reverse delay(1000); } 

在使用任何数学函数(如abs();之前,还要使用#include abs();

最重要的是,您必须让您的编译器了解您正在使用的function。

您需要在调用函数之前将函数的声明设置为rotaterotateDeg等。 或者更好的是,将函数声明放在标题中并将其包含在开头。

对于函数abs ,您需要include

你为什么得到这些警告 :鉴于这个简单的程序:

 int main(void) { func(); return 0; } void func(void) { //do something } 

编译器在看到它的声明之前看到了func ,因此编译器将生成一个隐式声明: int func(); ,其返回类型默认为int 。 那不是你的意思。 要更正它,请使用:

 void func(void); int main(void) { func(); return 0; } void func(void) { //do something } 

另请注意,隐含的函数声明在C89中是合法的,但已在C99中删除。

在使用之前,您必须让编译器了解该函数。 您通常在包含的.h文件中执行此操作,但您也可以在调用之前编写完整的函数。

C编译器是一次通过编译器。 它从一开始就开始,当它结束时,就完成了。 当它在您告诉编译器它存在之前看到您使用该函数时,您会收到此错误/警告。

你可以简单地说

 void my_func(int); 

在代码的顶部,然后编译器会知道你在代码中稍后调用my_func时的意思,即使它没有看到函数的实际主体。

之所以作为错误返回的原因是C不会提前读取函数声明。 相反,它会创建一个与您使用的签名匹配的隐式函数。

代码rotateDeg(-360, .1)创建一个隐式函数,其方法签名为roatateDeg(int deg, double speed)因为这些数字文字解析器的类型。

要解决此问题,请添加该行

 void rotateDeg(float deg, float speed); 

在导入之后到代码的顶部。 这在使用之前显式声明了该方法,并删除了方法冲突和implicit declaration警告。

另一种解决方案是简单地将您的功 这在编写函数时很有用,并且您没有修复函数参数和类型。

在建立函数头之后,如前所述,将原型放在main之前或将它们放在头文件中并包含它。

Interesting Posts