Search code examples
cc99implicit-declaration

Why does my compiler think my readlink() is implicitly declared if I set the standard to c99 or c11?


I have toy code that looks like this

#include <stdlib.h>                                                                         
#include <unistd.h>                                                                         
                                                                                         
int main()                                                                                  
{                                                                                           
    readlink("/proc/self/exe", "/my/path", 128);                                            
                                                                                         
    return EXIT_SUCCESS; 
}

When I compile with

icc main.c -o helloworld

everything is fine but when I e.g. try

icc -std=c99 main.c -o helloworld

or

icc -std=c11 main.c -o helloworld

I get the error message

main.c(6): warning #266: function "readlink" declared implicitly

What is it about c11 (or c99) standards that induces this error?


Solution

  • The definition is wrapped in

    #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
    

    From the man page for readlink you need to set the proper source definition first. The current POSIX definition can be set with gcc -std=c11 -D_POSIX_C_SOURCE=200809L If you don't set everything correctly you get to hunt undefined behavior because sizeof(int) and sizeof(void*) aren't the same anymore. Implicit declarations really did need to go for 64 bit to become.

    -std=gnu11 flips everything on. If you don't have to care if you accidentally use a gcc extension or not, just set it in your makefile and forget about it.