My compiler (GCC) is giving me the warning:
warning: implicit declaration of function
Why is it coming?
![]()
asked Dec 9, 2011 at 3:49
2
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);
answered Dec 9, 2011 at 3:50
cnicutarcnicutar
176k25 gold badges358 silver badges389 bronze badges
15
The right way is to declare function prototype in header.
Example
main.h
#ifndef MAIN_H
#define MAIN_H
int some_main(const char *name);
#endif
main.c
#include "main.h"
int main()
{
some_main("Hello, Worldn");
}
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
}
VLL
9,2851 gold badge28 silver badges54 bronze badges
answered Dec 3, 2014 at 14:26
![]()
When you do your #includes in main.c, put 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 «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 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 different order.
Well, it did for me.
answered Nov 9, 2016 at 12:04
tomctomc
1111 silver badge2 bronze badges
0
You need to declare the desired function before your main function:
#include <stdio.h>
int yourfunc(void);
int main(void) {
yourfunc();
}
answered Feb 8, 2020 at 12:48
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 http://linux.die.net/man/ This is 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.
answered Nov 26, 2015 at 7:59
![]()
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
answered Aug 28, 2015 at 23:05
2
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.
answered Aug 2, 2021 at 11:31
![]()
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.
![]()
answered Dec 15, 2020 at 9:23
ChrisChris
192 bronze badges
1
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.
![]()
answered Jul 26, 2022 at 22:19
1
Problem:
While trying to compile your C/C++ program, you see an error message like
../src/main.c:48:9: error: implicit declaration of function 'StartBenchmark' [-Werror=implicit-function-declaration]
StartBenchmark();
Solution:
implicit declaration of function means that you are trying to use a function that has not been declared. In our example above, StartBenchmark is the function that is implicitly declared.
This is how you call a function:
StartBenchmark();
This is how you declare a function:
void StartBenchmark();
The following bullet points list the most common reasons and how to fix them:
- Missing
#include: Check if the header file that contains the declaration of the function is#included in each file where you call the function (especially the file that is listed in the error message), before the first call of the function (typically at the top of the file). Header files can be included via other headers, - Function name typo: Often the function name of the declaration does not exactly match the function name that is being called. For example,
startBenchmark()is declared whileStartBenchmark()is being called. I recommend to fix this by copy-&-pasting the function name from the declaration to wherever you call it. - Bad include guard: The include guard that is auto-generated by IDEs often looks like this:
#ifndef _EXAMPLE_FILE_NAME_H #define _EXAMPLE_FILE_NAME_H // ... #endif
Note that the include guard definition
_EXAMPLE_FILE_NAME_His not specific to the header filename that we are using (for exampleBenchmark.h). Just the first of all header file names wil - Change the order of the
#includestatements: While this might seem like a bad hack, it often works just fine. Just move the#includestatements of the header file containing the declaration to the top. For example, before the move:#include "Benchmark.h" #include "other_header.h"
after the move:
#include "Benchmark.h" #include "other_header.h"
We are very much familiar with the flow control in C where it follows the Top-Down approach, and when we are writing a C program and if we are using any function, we might come across a very common error ‘Implicit declaration of function’.
Now why this error occurred? The answer is already in the error.
We have used a function in our program which is not declared yet or we can say that we have used a function implicitly.
Implicit declaration of the function is not allowed in C programming. Every function must be explicitly declared before it can be called.
In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration.
Here is a small code that will give us an Implicit declaration of function error.
|
#include <stdio.h> int main(void) { int a = 10; int b = 20; printf(«The value of %d + %d is %d»,a, b, addTwo(10, 20)); return 0; } |
Now the above code will give you an error of Implicit declaration.
|
clang-7 -pthread -lm -o main main.c main.c:6:45: warning: implicit declaration of function ‘addTwo’ is invalid in C99 [-Wimplicit-function-declaration] printf(«The value of %d + %d is %d»,a, b, addTwo(10... ^ 1 warning generated. /tmp/main-c6e933.o: In function `main‘: main.c:(.text+0x38): undefined reference to `addTwo’ clang-7compiler exit status 1 : error: linker command failed with exit code 1 (use -v to see invocation) |
Here are some of the reasons why this is giving error.
- Using a function that is pre-defined but you forget to include the header file for that function.
- If you are using a function that you have created but you failed to declare it in the code. It’s better to declare the function before the main.
In C90, this error can be removed just by declaring your function before the main function.
For example:
|
#include <stdio.h> int yourFunctionName(int firstArg, int secondArg); int main() { // your code here // your function call } int yourFunctionName(int firstArg, int secondArg) { // body of the function } |
In the case of C99, you can skip the declaration but it will give us a small warning and it can be ignored but the definition of the function is important.
|
#include <stdio.h> // optional declaration int main(void) { int a = 10; int b = 20; printf(«The value of %d + %d is %d»,a, b, addTwo(10, 20)); return 0; } int addTwo(int a, int b) { return a + b; } |
This gives us the output:
|
clang-7 -pthread -lm -o main main.c main.c:6:45: warning: implicit declaration of function ‘addTwo’ is invalid in C99 [-Wimplicit-function-declaration] printf(«The value of %d + %d is %d»,a, b, addTwo(10... ^ 1 warning generated. ./main The value of 10 + 20 is 30 |
Here you can see that our code is working fine and a warning is generated but we are good to go but this is not recommended.
Well, this was all about the Implicit declaration of the function in C and the error related to it. If you are stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug on your own as someone might have faced a similar problem earlier.
If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.
Thanks for your visit and if you are new here, consider subscribing to our newsletter. See you in my next post. Bye!
“A young man is smoking one cigarette after each other without a pause. An elderly woman observes that and says: “Young man, you are smoking like crazy! Don’t you know that there is a warning on each cigarette package that this can kill you?” The young man finishes his cigarette, looks at the elderly person and says: “Yes, I know. But look, I’m a programmer, and it is only a warning.”
I don’t smoke, and I do pay attention to warnings :-). I always try to keep my source code free of compiler warnings. And I always pay special attention to the following on:

implicit declaration of function
So what does the gcc warning ‘implicit declaration of function’ mean (other compiler report a ‘implicit parameter declaration’)? Basically it means that the compiler has found a call to function for which he does not have a prototype. For the case it missed a declaration like this:
errorCode_t calcMatrix(matrix_t *m, double weight);
Either because I did not provide a ‘forward declaration’ of it or I missed to include the header file with that declaration, or that declaration is missing in the header file.
Without knowing the interface, the compiler has to assume (implicitly) the interface from the actual parameters used, and that the function returns an int. So the compiler has to assume according to the rules:
int calcMatrix(int*, int);
Which of course is wrong in many ways: not only the parameter 3 is not converted to a double, it means that the wrong size (int vs. double) is assumed. If that parameter is passed on the stack it will result in a stack space mismatch, so overall very bad. It might ‘work’ if the types by chance match the actual implementation or will be less of a runtime problem if parameters and return values are passed in registers. Still, it is clearly wrong and bad, bad, bad. It might be only a warning if the linker does not find that symbol (calcMatrix in this case) somewhere, so if you do not pay attention to warnings, your application might crash or behave in wrong way.
Such an ‘implicit declaration’ is really an oversight or error by the programmer, because the C compiler needs to know about the types of the parameters and return value to correctly allocate them on the stack. Unfortunately in C this is not an error but a warning (for legacy reasons, to be able to compile old non-compliant code). The good news is that compiling such a thing with in C++ will produce an error, another good reason to compile plain C code with C++ mode.
The other good news is that every C compiler usually has an option to turn this warning into an error (what I recommend if you are not paying attention to warnings). From the gcc help page:
-Werror-implicit-function-declarationGive a warning (or error) whenever a function is used before being declared. The form -Wno-error-implicit-function-declaration is not supported. This warning is enabled by -Wall (as a warning, not an error).
With this option added to the compiler settings it gets flagged as an error:

error for implicit declaration
Problem solved.
Happy warning 🙂
Links
- GNU gcc warning options: Warning Options – Using the GNU Compiler Collection (GCC)
This tutorial discusses removing the warning of implicit function declaration by declaring the functions above the main function in C.
Implicit Declaration of Function in C
Sometimes, the compiler shows a warning of implicit declaration of the function in C language, which means that the function is not declared on top of the main() function or its header file is not included.
For example, the printf() function belongs to the stdio.h header file, and if we don’t include it before using it in a C source file, the compiler will show a warning that the function declaration is implicit.
In this case, we have to include the header file, which includes the function declaration or declare the function above the main() function. For example, let’s use the printf() function without including its header file.
See the code below.
int main()
{
int a = 10;
printf("a = %d",a);
return 0;
}
Output:
main.c: In function 'main':
main.c:5:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
5 | printf("a = %d",a);
| ^~~~~~
main.c:5:5: warning: incompatible implicit declaration of built-in function 'printf'
main.c:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'
+++ |+#include <stdio.h>
1 |
a = 10
In the above output, we can see that the compiler gave a warning saying that the declaration of the printf() function is implicit and we have to include the <stdio.h> file, or we must provide a declaration of the printf() function in the source file.
We can also see that the value of a is printed, which means the printf() function worked perfectly even though we have not included its header file.
Now, let’s include the header file and repeat the above example. See the code below.
#include <stdio.h>
int main()
{
int a = 10;
printf("a = %d",a);
return 0;
}
Output:
We can see above the warning of the implicit function declaration is not shown this time because we have included the header file for the printf() function.
The implicit function declaration warning will also be shown if we have created a function in a source file but have not declared it above the main() function.
The compiler warns when we try to call the function that the function declaration is implicit. For example, let’s create a function and call it from the main() function without declaring it above the main() function.
#include <stdio.h>
int main()
{
int a = fun(2, 3);
printf("a = %d",a);
return 0;
}
int fun(int x, int p)
{
int a = x+p;
return a;
}
Output:
main.c: In function 'main':
main.c:4:13: warning: implicit declaration of function 'fun' [-Wimplicit-function-declaration]
4 | int a = fun(2, 3);
| ^~~
a = 5
In the above output, we can see that the warning is shown for the function we created. We have to declare the function above the main() function to solve this problem.
For example, let’s declare the function above the main() function and repeat the above example. See the code below.
#include <stdio.h>
int fun(int x, int p);
int main()
{
int a = fun(2, 3);
printf("a = %d",a);
return 0;
}
int fun(int x, int p)
{
int a = x+p;
return a;
}
Output:
We can see that the warning is now gone. We can also declare the function in a header file and then include the header file in the source file, which is useful in the case of many functions because it will simplify the code.
|
0 / 0 / 0 Регистрация: 17.05.2015 Сообщений: 6 |
|
|
1 |
|
|
13.06.2015, 16:21. Показов 26989. Ответов 6
Qt Creator выдает ошибку:
__________________
0 |
|
55 / 55 / 39 Регистрация: 19.03.2015 Сообщений: 167 |
|
|
13.06.2015, 16:37 |
2 |
|
скорее всего какой то заголовок не включен.
0 |
|
0 / 0 / 0 Регистрация: 17.05.2015 Сообщений: 6 |
|
|
13.06.2015, 16:38 [ТС] |
3 |
|
А включить можно? Ну или сделать по другому, но чтобы работало?
0 |
|
528 / 430 / 159 Регистрация: 25.11.2014 Сообщений: 1,662 |
|
|
13.06.2015, 16:43 |
4 |
|
implicit declaration of function ‘gotoxt’ Если мне не изменяет память, то это из борланда. conio.h или что-то подобное.
можно это как-то исправить? Компилировать в стареньком Borland C++.
0 |
|
0 / 0 / 0 Регистрация: 17.05.2015 Сообщений: 6 |
|
|
13.06.2015, 16:45 [ТС] |
5 |
|
скорее всего какой то заголовок не включен Ладно, плохо сказала. Заголовок включила, эта проблема ушла, осталась другая: undefined reference to ‘gotoxt’/’textattr’/’clrscr’
0 |
|
528 / 430 / 159 Регистрация: 25.11.2014 Сообщений: 1,662 |
|
|
13.06.2015, 16:46 |
6 |
|
undefined reference to Это уже ошибка линковщика. Подключи ему библиотеку, которая связана с .h, который ты подключила.
0 |
|
0 / 0 / 0 Регистрация: 17.05.2015 Сообщений: 6 |
|
|
13.06.2015, 16:55 [ТС] |
7 |
|
Добавлено через 8 минут
Подключи ему библиотеку, которая связана с .h, который ты подключила А где найти эту библиотеку и как подключить??
0 |
-
01-26-2006
#1

Registered User
- Join Date
- Jan 2006
- Posts
- 13
Implicit declaration of function …???
Hi,
I keep getting the warning of:Code:
Implicit declaration of funciton getline()
whenever i use getline() function
What does this warning mean actually ?
Is this a serious warning, or just ignorable.Cheer!!
-
01-26-2006
#2

Registered User
- Join Date
- Aug 2005
- Posts
- 1,267
getline() is c++ not C. There is no standard C getline() function.
-
01-26-2006
#3

Registered Luser
- Join Date
- Jul 2005
- Location
- Sydney, Australia
- Posts
- 869
getline() is also a GNU provided extension available under #include <stdio.h>. It will not appear if you use the -ansi flag, because the -ansi flag excludes all GNU extensions. It’s not part of standard C, as Ancient Dragon said.
-
01-26-2006
#4

Registered User
- Join Date
- Aug 2005
- Posts
- 1,267
replace getline() with fgets() and your program will (maybe) work.
-
01-27-2006
#5

Registered User
- Join Date
- Jun 2005
- Posts
- 6,815
What’s happening is that you have tried to use a function called getline() that hasn’t been declared anywhere (eg in a header file) and the compiler takes your attempt to call it as an implicit declaration.
As other have sort of said, your real problem is probably that you’re calling a function that doesn’t exist and you need to work out what function you really intend to call. If the function is one you actually know you’re trying to call, find the corresponding header file and #include it.
-
01-28-2006
#6

Frequently Quite Prolix
- Join Date
- Apr 2005
- Location
- Canada
- Posts
- 8,057
Code:
$ grep -n getline /usr/include/*.h

If you’re trying to use the C++ getline, then you need <iosteam> etc.
dwk
Seek and ye shall find. quaere et invenies.
«Simplicity does not precede complexity, but follows it.» — Alan Perlis
«Testing can only prove the presence of bugs, not their absence.» — Edsger Dijkstra
«The only real mistake is the one from which we learn nothing.» — John PowellOther boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.netMy website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
-
08-07-2006
#7

Registered User
- Join Date
- Aug 2006
- Posts
- 12
Sorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
The answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h. As some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension. In order to correctly include these extensions you have to add a define:Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Hope this clarifies the issue.
— LT
-
08-07-2006
#8

Registered User
- Join Date
- Jun 2005
- Posts
- 6,815
Originally Posted by Lord ThunderSorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
Depends on your perspective. I consider your post to be incorrect and misleading, as you make some statements early on which are compiler/library specific but you state them as generalities. Yes you correct them later, sort of, but the information you give is not generally true.
Originally Posted by Lord ThunderThe answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h.No. The functions that are declared in <stdio.h> are specified in the ISO C standard. They are not required to be defined in stdio.h.
Reliance on stdio.h declaring any function that is not specified in the C standard is invalid, unless you are prepared to constrain yourself to only one compiler and it’s library (and possibly to a particular version of that compiler and library).
Originally Posted by Lord ThunderAs some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension.
Exactly my point. There are a few commonly used compilers that are produced by vendors not associated with GNU. Most of those compilers do not support GNU extensions and are not bunded with glibc.
Originally Posted by Lord ThunderIn order to correctly include these extensions you have to add a define:
Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Sort of. glibc, in practical terms, often relies on using a correct version of gcc (the GNU compiler collection). Several versions of glibc only work with particular versions of gcc. This is the reason that the gnu compiler and glibc are often bundled together, particularly with recent versions. Both the gnu compiler and library are supported on more systems than linux; cygwin is just one of several windows ports.
-
08-07-2006
#9

…
- Join Date
- Jan 2003
- Posts
- 1,534
Originally Posted by Lord ThunderSorry if I am a topic digging here, but I came across this thread while searching for the answer for exactly this problem and I noticed noone here gave the right answer.
The answer turned out pretty simple:
getline() IS a valid C function which is defined in stdio.h. As some people rightfully suggested it is not a function that is defined in the ANSI standard, but it is a GNU extension. In order to correctly include these extensions you have to add a define:Code:
#define _GNU_SOURCE #include <stdio.h> /* functions like getline() and getdelim() should be defined now */
Ofcourse this only works if you are actually using glibc (e.g. on Linux or cygwin).
Hope this clarifies the issue.
— LTWhy is it exactly that you are dredging up a 7 month old thread? Was the urge so strong to blabber that you could not resist? Give me a break.
Forum Guidelines. Read before posting:
5. Don’t bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
Any questions?
