Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning Implicit Declaration of posix_memalign

Tags:

c

linux

posix

gcc

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?

like image 727
chasep255 Avatar asked Sep 07 '15 12:09

chasep255


1 Answers

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.

like image 110
Nominal Animal Avatar answered Oct 21 '22 01:10

Nominal Animal