C语言 不断收到隐式声明错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17871338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:01:07  来源:igfitidea点击:

keep getting implicit declaration error

carduinoraspberry-pi

提问by Yamaha32088

I keep getting these errors when compiling. I modified the code that runs on an arduino to run on my raspberry pi.

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

Here is my source code:

这是我的源代码:

#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); 
  } 
}

回答by 0decimal0

You get an implicit declaration warning when there is an implicitly declared function.

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

An implicitly declared function is a function which has neither a prototype nor a definition and that's why a compiler cannot verify that what do you want to do with the function.

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

If no prior declaration of a function is available then its first instance is assumed to be a declaration implicitly with return type intand nothing is assumed about the parameters.

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

Just leave a declaration of functions rotateand rotatedeglike this :

刚刚离开的函数的声明rotaterotatedeg这样的:

void rotate (int , float );

and

void rotateDeg (float , float );

Before using it in loop :

在循环中使用它之前:

void loop(){ 

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

Also use #include<math.h>before using any mathematical functions like abs();.

还可以使用#include<math.h>使用像任何数学函数之前abs();

The bottom line is , you have to make your compiler know about the functions you are using.

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

回答by Yu Hao

You need to put the declarations of functions rotateand rotateDegetc before calling them. Or better, put the function declarations in a header and include it in the beginning.

你需要把函数的声明rotaterotateDeg等调用它们。或者更好的是,将函数声明放在标题中并将其包含在开头。

For the function abs, you need to include <math.h>

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

Why you are getting these warnings: Given this simple program:

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

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

The compiler saw funcbefore saw its declaration, so compiler will generate an implicit declaration: int func();, its return type is intby default. That's not what you meant. To correct it, use this:

编译器func之前看到了它的声明,所以编译器会生成一个隐式声明:int func();int默认返回类型。那不是你的意思。要纠正它,请使用:

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

Also note that implicit declaration of function is legal in C89, but has been removed in C99.

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

回答by cptroot

The reason this is coming back as an error is that C doesn't read ahead to find function declarations. Instead, it creates a implicit function that matches the signature you used.

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

The code rotateDeg(-360, .1)creates an implicit function with a method signature of roatateDeg(int deg, double speed)because of the types that those number literals parser to.

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

To solve this problem, add the line

要解决此问题,请添加行

void rotateDeg(float deg, float speed);

to the top of your code, after the imports. This explicitly declares the method before its use, and removes both the method conflict and the implicit declarationwarning.

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

回答by xaxxon

You have to let the compiler know about the function before you use it. You normally do this in a .h file that you include, but you can simply write the full function before you call it, as well.

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

The C compiler is a one-pass compiler. It starts at the beginning and when it gets to the end, it's done. When it sees you using the function before you've told the compiler that it exists, then you get this error/warning.

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

You can simply say

你可以简单地说

void my_func(int); 

at the top of your code, and then the compiler will know what you mean when you call my_func later in your code, even if it hasn't seen the actual body of the function.

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

回答by LS_???

Another solution is simply put your functions before main. This is useful while writing function and you have not fixed function parameters and types.

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

After establishing functions headers, as already told, put prototypes before main or put them in a header file and include it.

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