Search code examples
c++c++11g++cygwin

Error compiling CPP application. "error: 'posix_memalign' was not declared in this scope"


I'm trying to compile a CPP application (an open source project) in the latest cygwin64 environment using g++ 6.4.0 and I get the following error:

error: 'posix_memalign' was not declared in this scope

now posix_memlign can be found in stdlib.h if you compile the most simple CPP "hello world" application there wouldn't be a problem calling posix_memlign.

The make file of the project report the following setup for the compilation

g++ -DHAVE_CONFIG_H -I. -Wall -Wnon-virtual-dtor -I. -I./include -g -O3 -std=c++0x -g -O3 -std=c++0x -MT lib/rectangular_binary_matrix.lo -MD -MP -MF lib/.deps/rectangular_binary_matrix.Tpo -c lib/rectangular_binary_matrix.cc -DDLL_EXPORT -DPIC -o lib/.libs/rectangular_binary_matrix.o

so it doesn't look like it override the default include path. Any ideas?

p.s. I was able to build the code on Linux (Redhat) without a problem.


Solution

  • posix_memalign is not part of the C Standard Library or the C++ Standard library and the cygwin GCC compilers do not provide it, although other compilers may do so, including GCC compilers from other builders.

    You might consider using instead the C Standard function aligned_alloc, if you feel comfortable to edit your project source. It is provided in <cstdlib> for C++ compilation in cygwin g++ 6.4.0

    Later

    I do see the function in C:\cygwin64\usr\include\stdlib.h...

    The fact that you can find the function declaration in the header file does not mean that the compiler can see it after preprocessing. The same source header may be used by many builders, exposing different declarations to the compiler depending on the builder's settings of implementor macros. In this case, the declaration is concealed from your compiler by the fact that __POSIX_VISIBLE >= 200112 is false. Identifiers beginning __ are reserved for implementors. See the explanation of this macro and note the comment:

     * The following private macros are used throughout the headers to control
     * which symbols should be exposed.  They are for internal use only, as
     * indicated by the leading double underscore, and must never be used outside
     * of these headers.