305

My compiler (GCC) is giving me the warning:

warning: implicit declaration of function

Why is it coming?

4
  • 4
    A "why does it not give an error version": stackoverflow.com/questions/434763/… Commented May 13, 2015 at 7:27
  • 3
    This can also happen if you forget to include a header file. For example if you are trying to use strlen() without including string.h you will get this error Commented Sep 11, 2016 at 8:21
  • I think this question should be removed, as it clutters up every search for a specific instance of this warning. e.g. "My kernel stopped building today, let's see if others are having a similar issue..." Inevitably I end up here. Commented Dec 4, 2023 at 19:51
  • i landed here because i tried unit testing a c source and did not see that some functions were private. i should've paid more attention to the header file. also, our compiler is set to strictly treat every warning as error. in general, it is wise to prefix private functions with an underscore, like _myPrivFunc Commented Jul 8, 2024 at 14:17

10 Answers 10

348

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);
Sign up to request clarification or add additional context in comments.

15 Comments

As an addition if you have given the prototype check that it isn't just a typo. Also if its from an external library check that you have included it.
I cannot run the code after I get this warning. So it behaves like an error.
@Flimm, C99 and C89/C90 has different setting for this
@ZachSaw Rightly so. Else you would not have repeated yourself thrice.
|
35

The right way is to declare the function prototype in a 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, 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
}

Comments

12

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.

1 Comment

Why two includes in one line? - #include "system.h" #include <stdio.h>.
4

You need to declare the desired function before your main function:

#include <stdio.h>
int yourfunc(void);

int main(void) {

   yourfunc();
 }

Comments

3

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.

1 Comment

Does this add anything not already provided by the other answers?
3

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

2 Comments

The first link is half broken. The second link is completely broken ("Hmm. We’re having trouble finding that site. We can’t connect to the server at patchwork.alpinelinux.org.").
Alpine uses Gitlab nowadays - links updated
3

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.

Comments

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.

Comments

0

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.

1 Comment

Re "...it can find": Don't you mean "...it can’t find" (the opposite)?
0

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.

1 Comment

The first reference says "If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof. "

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.