I’m getting a warning from my GCC compiler that says “implicit declaration of function.” What could be causing this warning to appear?
The warning “implicit declaration of function” occurs when you use a function without providing a declaration (also known as a prototype) before the function is called.
int main() {
fun(2, "21"); // The compiler has not seen the declaration for 'fun'.
return 0;
}
int fun(int x, char *p) {
// Function definition
}
To resolve this warning, you need to declare your function before main, either directly or in a header file:
int fun(int x, char *p); // Declaration of 'fun'
int main() {
fun(2, "21");
return 0;
}
int fun(int x, char *p) {
// Function definition
}
This way, the compiler knows about the function’s signature before it is called, preventing the warning.
To avoid the “implicit declaration of function” warning, you should declare the desired function before your main function. Here’s an example:
#include <stdio.h>
// Declaration of the function
int yourfunc(void);
int main(void) {
// Call to the function
yourfunc();
return 0;
}
This way, the compiler knows about the function yourfunc before it’s used in main, preventing the warning.
If the function is declared in a header file, make sure to include that header file at the beginning of your source file.
#include "myHeader.h" // Include the header file that contains the function declaration
int main() {
// Function call
myFunction();
return 0;
}
// Function definition
void myFunction() {
// Function body
}
E.g., say this is main.c and your referenced function is in file "SSD1306_LCD.h":
#include "SSD1306_LCD.h"
#include "system.h" #include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
The above will not generate the "implicit declaration of function" error, but the below will:
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <string.h>
#include <math.h>
#include <libpic30.h> // http://microchip.wikidot.com/faq:74
#include <stdint.h>
#include <stdbool.h>
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition
#include "SSD1306_LCD.h"
Exactly the same #include list, just a different order.
Well, it did for me.