【问题标题】:keep getting implicit declaration error不断收到隐式声明错误
【发布时间】:2013-07-26 01:16:10
【问题描述】:

我在编译时不断收到这些错误。我修改了在 arduino 上运行的代码以在我的树莓派上运行。

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 <wiringPi.h>
#include <stdio.h>
#include <stdio.h>

#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 < steps; i++){ 
    digitalWrite(STEP_PIN, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(STEP_PIN, LOW); 
    delayMicroseconds(usDelay); 
  } 
} 

void rotateDeg(float deg, float speed){ 
  //rotate a specific number of degrees (negitive for reverse movement)
  //speed is any number from .01 -> 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); 
  } 
}

【问题讨论】:

    标签: c arduino raspberry-pi


    【解决方案1】:

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

    隐式声明的函数是一个既没有 原型也不是定义,这就是编译器无法验证的原因 那你想用这个函数做什么。

    如果没有可用的函数的先前声明,则假定其第一个实例是隐式声明,返回类型为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&lt;math.h&gt;

    底线是,你必须让你的编译器知道你正在使用的函数。

    【讨论】:

    • arduino IDE 对用户隐藏了这一要求,使编写简单的 arduino 草图变得更简单,但正如您所发现的,以它不是标准 C 为代价。
    【解决方案2】:

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

    函数abs,需要include &lt;math.h&gt;

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

    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 中已被删除。

    【讨论】:

      【解决方案3】:

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

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

      你可以简单地说

      void my_func(int); 
      

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

      【讨论】:

        【解决方案4】:

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

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

        要解决这个问题,添加行

        void rotateDeg(float deg, float speed);
        

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

        【讨论】:

          【解决方案5】:

          另一种解决方案是将您的函数简单地放在main 之前。这在编写函数时很有用,并且您没有固定函数参数和类型。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多