My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
For example:
int main() {
fun(2, "21"); /* The compiler has not seen the declaration. */
return 0;
}
int fun(int x, char *p) {
/* ... */
}
You need to declare your function before main, like this, either directly or in a header:
int fun(int x, char *p);
gcc -std=c11 -pedantic-errors
. The C standard never requires a program to be rejected unless it has a #error
directive. –
Probable #error
). If you're using gcc or clang, you can use -pedantic-errors
or -Werror
. Or you can just fix your code so it doesn't produce any warnings. –
Probable The right way is to declare the function prototype in a header.
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, World\n");
}
int some_main(const char *name)
{
printf("%s", name);
}
Alternative with one file (main.c)
static int some_main(const char *name);
int some_main(const char *name)
{
// do something
}
When you do your #includes in file main.c, add the #include reference to the file that contains the referenced function at the top of the include list.
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.
#include "system.h" #include <stdio.h>
. –
Moina You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
When you get the "error: implicit declaration of function", it should also list the offending function.
Often this error happens because of a forgotten or missing header file, so at the shell prompt you can type man 2 functionname
and look at the SYNOPSIS
section at the top, as this section will list any header files that need to be included. Or try man. Those are the online man pages. They are hyperlinked and easy to search.
Functions are often defined in the header files, including any required header files is often the answer. Like cnicutar said,
You are using a function for which the compiler has not seen a declaration ("prototype") yet.
Don't forget, if any functions are called in your function, their prototypes must be situated above your function in the code. Otherwise, the compiler might not find them before it attempts to compile your function. This will generate the error in question.
If you have the correct headers defined & are using a non glibc library (such as Musl C), GCC will also throw "error: implicit declaration of function" when GNU extensions such as malloc_trim
are encountered.
The solution is to wrap the extension & the header:
#if defined (__GLIBC__)
malloc_trim(0);
#endif
This error occurs because you are trying to use a function that the compiler does not understand. If the function you are trying to use is predefined in C language, just include a header file associated with the implicit function. If it's not a predefined function then it's always a good practice to declare the function before the main function.
I think the question is not 100% answered. I was searching for an issue with a missing typeof(), which is a compile-time directive.
The following links will shine light on the situation:
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html
https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords
As of a conclusion, try to use __typeof__()
instead. Also gcc ... -Dtypeof=__typeof__ ...
can help.
__typeof__
instead of typeof
. " –
Moina The GNU C compiler is telling you that it can find that particular function name in the program scope. Try defining it as a private prototype function in your header file, and then import it into your main file.
© 2022 - 2024 — McMap. All rights reserved.