monkeyFly
monkeyFly

Reputation: 49

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?

Upvotes: 1

Views: 497

Answers (1)

Jonas Sch&#228;fer
Jonas Sch&#228;fer

Reputation: 20708

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;
}

Upvotes: 3

Related Questions