Aligned_alloc not found for clang

I am running the following version of clang on a Mac OS X host:

$ clang -v Apple LLVM version 8.1.0 (clang-802.0.42) 

I have code that uses the aligned_alloc() C11 function to allocate a aligned piece of memory.

I compile my binary with the flag -std=c11 :

 ... clang -g -Wall -Wextra -mavx -std=c11 -D__USE_POSIX -D__STDC_CONSTANT_MACROS -D__STDINT_MACROS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -O2 -c my_binary.c -o my_binary.o; \ clang -g -Wall -Wextra -mavx -std=c11 -D__USE_POSIX -D__STDC_CONSTANT_MACROS -D__STDINT_MACROS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -O2 my_binary.o -o my_binary -lm; \ ... 

I turn on stdlib.h and add POSIX flags. From my_binary.h :

 ... #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L #endif /* getline() support */ #ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 600 #endif /* aligned_alloc() support */ #include <stdlib.h> ... 

I get the following compilation warning:

 my_binary.c:245:15: warning: implicit declaration of function 'aligned_alloc' is invalid in C99 [-Wimplicit-function-declaration] s->data = aligned_alloc(32, s->n * sizeof(*s->data)); 

What follows this error:

 Undefined symbols for architecture x86_64: "_aligned_alloc", referenced from: _bs_initialize_signal_avx in my_binary.o _bs_copy_signal_avx in my_binary.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [build] Error 1 

What am I doing wrong with clang so that compilation ignores the std C11 flag?

I can compile without errors on a CentOS 7 (Linux) host with gcc 5.3.0 and glibc 2.22.

+5
source share

Source: https://habr.com/ru/post/1269363/


All Articles