aligned_alloc not found for clang

Viewed 2253

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 some code that uses the aligned_alloc() C11 function to allocate an aligned chunk of memory.

I am compiling my binary with the -std=c11 flag:

...
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 am including stdlib.h and adding 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));

Which follows with 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 incorrectly with clang, such that compilation ignores the std C11 flag?

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

0 Answers
Related