chasep255
chasep255

Reputation: 12175

Warning Implicit Declaration of posix_memalign

I am using GCC 4.9 on ubuntu 15.04. I am coding in eclipse CDT. This is a C program with the dialect set to c99. For some reason my compiler keeps warning me about this...

warning: implicit declaration of function ‘posix_memalign’ [-Wimplicit-function-declaration]

I am not sure why. I have #include<stdlib.h> at the top and when I use eclipse the ctrl+click posix_memalign it takes me to the function declaration in stdlib.h. Why am I getting this warning?

I just tried changing the dialext to std=gnu99 and this fixed the issue. Is posix_memalign not included with c99?

Upvotes: 5

Views: 4414

Answers (1)

Nominal Animal
Nominal Animal

Reputation: 39316

The #define _POSIX_C_SOURCE 200809L and other feature test macros have to be defined before any #include lines.

This is because the macros tell the standard C library headers which features it should provide in addition/instead of the standard C library features; the features are "locked" at the point of #include.

posix_memalign() is provided by stdlib.h, but only if POSIX.1-2001 or later are enabled; this means defining _POSIX_C_SOURCE as 200112L or larger (the L is there because it is an integer constant of long type), or _XOPEN_SOURCE with 600 or larger.

The error shown only occurs when

  1. The macros were not defined when stdlib.h was included

    or

  2. stdlib.h was not included

    or

  3. The C library implementation does not provide POSIX.1 features

Using GCC in Ubuntu, it has to be one of the first two, because the C library most definitely does provide these POSIX.1 features.

Upvotes: 6

Related Questions