Search code examples
clinuxcrypt

Why does this source code using "crypt" have this compiler warning:


#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>

int main()
{
    const char *key = NULL;
    const char *salt = NULL;
    crypt(key, salt);
    return 0;
}

use gcc test.c -o test -Wall -lcrypt to compile.

Which gives this warning:

initialization makes pointer from integer without a cast

Can anyone explain this warning and how to properly avoid it?


Solution

  • You have to put feature test macros before all includes. In your case, stdio.h already includes features.h behind the scenes, which takes care of converting the feature defines (like _XOPEN_SOURCE) into something internal the headers use. So when you include unistd.h, the flags have already been set and won’t be interpreted again, thus, declaring _XOPEN_SOURCE in the mean time won’t have any effect.

    Changing the order fixes the problem:

    #define _XOPEN_SOURCE
    #include <stdio.h>
    #include <unistd.h>
    
    int main()
    {
        const char *key = NULL;
        const char *salt = NULL;
        crypt(key, salt);
        return 0;
    }