warning: implicit declaration of function

前端 未结 7 811
旧时难觅i
旧时难觅i 2020-11-22 02:19

My compiler (GCC) is giving me the warning:

warning: implicit declaration of function

Please help me understand why is it coming

相关标签:
7条回答
  • 2020-11-22 02:56

    I think the question is not 100% answered. I was searching for issue with missing typeof(), which is compile time directive.

    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 conculsion try to use __typeof__() instead. Also gcc ... -Dtypeof=__typeof__ ... can help.

    0 讨论(0)
  • 2020-11-22 02: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
    
    0 讨论(0)
  • 2020-11-22 03:00

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

    #include <stdio.h>
    int yourfunc(void);
    
    int main(void) {
    
       yourfunc();
     }
    
    0 讨论(0)
  • 2020-11-22 03:04

    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, 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
    }
    
    0 讨论(0)
  • 2020-11-22 03:06

    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);
    
    0 讨论(0)
  • 2020-11-22 03:10

    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.

    0 讨论(0)
提交回复
热议问题