可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I keep getting these errors when compiling. I modified the code that runs on an arduino to run on my raspberry 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
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);
}
}
回答1:
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 int
and nothing is assumed about the parameters.
Just leave a declaration of functions rotate
and rotatedeg
like this :
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();
.
The bottom line is , you have to make your compiler know about the functions you are using.
回答2:
You need to put the declarations of functions rotate
and rotateDeg
etc before calling them. Or better, put the function declarations in a header and include it in the beginning.
For the function abs
, you need to 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 func
before saw its declaration, so compiler will generate an implicit declaration: int func();
, its return type is int
by default. That's not what you meant. To correct it, use this:
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.
回答3:
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.
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.
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.
回答4:
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.
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.
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 declaration
warning.
回答5:
Another solution is simply put your functions before main
. This is useful while writing function and you have not fixed function parameters and types.
After establishing functions headers, as already told, put prototypes before main or put them in a header file and include it.