Message "warning: implicit declaration of function"
Asked Answered
J

10

294

My compiler (GCC) is giving me the warning:

warning: implicit declaration of function

Why is it coming?

Joyejoyful answered 9/12, 2011 at 3:49 Comment(3)
A "why does it not give an error version": #435263Literal
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 errorSacci
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.Lona
E
333

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);
Environs answered 9/12, 2011 at 3:50 Comment(15)
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.Capparidaceous
I cannot run the code after I get this warning. So it behaves like an error.Arsis
@Flimm, C99 and C89/C90 has different setting for thisDismantle
@Flimm #435263Literal
C is a joke! Everything is done for you implicitly even when it makes absolutely no sense! Lots of warnings in C need to be errors for it to be any useful.Pairs
A prototype is a function declaration that specifies the types of any parameters. Not all function declarations are prototypes. (Arguably they all should be; non-prototype declarations are an obsolescent feature.)Probable
@Flimm: In C99 and later, it's a constraint violation, which means that the standard requires a diagnostic. That diagnostic is not required to be fatal. Many C compilers take advantage of that and issue non-fatal warnings for many constraint violations. Most compilers have options to produce fatal errors, for example gcc -std=c11 -pedantic-errors. The C standard never requires a program to be rejected unless it has a #error directive.Probable
@ZachSaw: You need to pay attention to warnings, or use an option to turn them into fatal errors.Probable
@KeithThompson: Hence "Lots of warnings in C need to be errors for it to be any useful."Pairs
@ZachSaw: Only if you don't pay attention to warnings. The C language standard doesn't distinguish between warnings and fatal errors (except for #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
@KeithThompson: Again, hence "Lots of warnings in C need to be errors for it to be any useful."Pairs
@ZachSaw: I'm not sure what repeating your earlier statement is supposed to accomplish. If the fact that many diagnostics are non-fatal makes C useless to you, even after you've been offered advice on how to deal with it, I don't think I can help you.Probable
@KeithThompson: In case it wasn't clear when I repeated it twice: you assumed I needed help.Pairs
@ZachSaw Rightly so. Else you would not have repeated yourself thrice.Kyungkyushu
You can also use static and not define it - but then this will cause fun to be see only in that specific file.Helle
O
33

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
}
Orvalorvan answered 3/12, 2014 at 14:26 Comment(0)
E
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.

Enfeoff answered 9/11, 2016 at 12:4 Comment(1)
Why two includes in one line? - #include "system.h" #include <stdio.h>.Moina
G
4

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

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

int main(void) {

   yourfunc();
 }
Gymnosophist answered 8/2, 2020 at 12:48 Comment(0)
A
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.

Amandine answered 26/11, 2015 at 7:59 Comment(0)
S
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.

Spinneret answered 15/12, 2020 at 9:23 Comment(1)
Does this add anything not already provided by the other answers?Lovable
R
2

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
Rosemarierosemary answered 28/8, 2015 at 23:5 Comment(2)
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.").Moina
Alpine uses Gitlab nowadays - links updatedRosemarierosemary
M
1

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.

Muscovado answered 2/8, 2021 at 11:31 Comment(0)
U
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.

Unprofitable answered 17/3, 2016 at 9:15 Comment(1)
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. "Moina
P
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.

Phraseograph answered 26/7, 2022 at 22:19 Comment(1)
Re "...it can find": Don't you mean "...it can’t find" (the opposite)?Moina

© 2022 - 2024 — McMap. All rights reserved.