8

编译时我不断收到这些错误。我修改了在 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); 
  } 
}
4

5 回答 5

10

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

隐式声明的函数是既没有原型也没有定义的函数,这就是为什么编译器无法验证您想对该函数做什么。

如果没有可用的函数的先前声明,则假定它的第一个实例是隐含的具有返回类型的声明,int并且不假定任何参数。

只需留下函数声明,rotate如下rotatedeg所示:

void rotate (int , float );

void rotateDeg (float , float );

在循环中使用它之前:

void loop(){ 

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

在使用#include<math.h>任何数学函数(如abs();.

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

于 2013-07-26T04:50:23.187 回答
3

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

对于该功能abs,您需要include <math.h>

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

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

于 2013-07-26T01:30:54.637 回答
1

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

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

你可以简单地说

void my_func(int); 

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

于 2013-07-26T01:30:41.353 回答
1

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

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

要解决此问题,请添加行

void rotateDeg(float deg, float speed);

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

于 2013-07-26T01:31:34.660 回答
1

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

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

于 2013-07-26T09:33:28.827 回答