1 Reply
      Latest reply on Feb 28, 2019 6:08 PM by Stokestack
      AlexReynolds Level 1 Level 1 (0 points)

        I am running the following version of Clang on a Mac OS X 10.12.5 host, installed via Xcode 8.3.3:


        $ 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


        #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? Or does the version of Clang shipped with Xcode 8.3.3 not support aligned_alloc()?


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

        • Re: C11 support for aligned_alloc()?
          Stokestack Level 1 Level 1 (0 points)

          Same problem. I'm using a third-party library that wants aligned buffers. Although I've selected C++17 as my language dialect, there's no aligned_alloc, not even the C11 one. I've tried including stdlib and cstdlib. No dice.  Seems like this should be available 7+ years after the fact.