Search code examples
c++android-ndkclang++

Android ndk-build error: undefined symbol: aligned_alloc


I am trying to build Shared Object (.so) files for the ImageMagick library. However, it get stuck due to the following error while creating the .so file:

[arm64-v8a] Executable     : magick
ld: error: undefined symbol: aligned_alloc
>>> referenced by memory.c:262 (././ImageMagick-7.0.9-17/MagickCore\memory.c:262)
>>>               memory.o:(AcquireAlignedMemory) in archive ./obj/local/arm64-v8a/libmagickcore-7.a
>>> referenced by memory.c:262 (././ImageMagick-7.0.9-17/MagickCore\memory.c:262)
>>>               memory.o:(AcquireVirtualMemory) in archive ./obj/local/arm64-v8a/libmagickcore-7.a
>>> referenced by memory.c:262 (././ImageMagick-7.0.9-17/MagickCore\memory.c:262)
>>>               memory.o:(AcquireVirtualMemory) in archive ./obj/local/arm64-v8a/libmagickcore-7.a
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [C:/hostedtoolcache/windows/ndk/r22b/x64/build//../build/core/build-binary.mk:741: obj/local/arm64-v8a/magick] Error 1
Error: Process completed with exit code 1.

Here's a GitHub Actions link where I am generating a .so file and for facing the error for ease of reproducing the issue.

https://github.com/malaythecool/Android-ImageMagick7/runs/2316777388?check_suite_focus=true


Solution

  • From the CI logs, it shows up:

    ././ImageMagick-7.0.9-17/MagickCore/memory.c:262:10: warning: implicit declaration of function 'aligned_alloc' is invalid in C99 [-Wimplicit-function-declaration]
    

    Which finally ends up in the linker complaining for the missing symbol:

    ld: error: undefined symbol: aligned_alloc
    

    Try adding the flag -std=c++1z to your build configuration since aligned_alloc() was introduced in C++17.

    It seems the Application.mk file already sets the -std=c++17 here. You may try adding the following flag too:

    APP_CONLYFLAGS += -std=c11
    

    to ensure that the C standard is updated to C11 wherein alloc_aligned() was introduced.