1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
мalay мeнтa
  • 125
  • 3
  • 11
  • 1. Do you know which library provides the implementation of `aligned_alloc`? 2. Do you know if that library is getting linked to your final `.so`? – Zoso Apr 11 '21 at 12:21
  • Also on what basis is `MAGICKCORE_HAVE_STDC_ALIGNED_ALLOC` defined? That's what forces the choice of the implementation in `memory.c` to choose the `aligned_alloc()` route. – Zoso Apr 11 '21 at 12:33
  • aligned_alloc is actually c++ function https://en.cppreference.com/w/c/memory/aligned_alloc. Moreover it's like going to fall in dark well if I change anything in code related to MAGICKCORE_HAVE_STDC_ALIGNED_ALLOC – мalay мeнтa Apr 11 '21 at 15:59
  • Yes, exactly my point. That should help you figure out what might be the problem. `aligned_alloc()` wasn't there in earlier versions of C, so you need to check the library i.e. standard C library whether it ships with that implementation on your system. Also, if you've checked the warnings in your CI `././ImageMagick-7.0.9-17/MagickCore/memory.c:262:10: warning: implicit declaration of function 'aligned_alloc' is invalid in C99 [-Wimplicit-function-declaration]`. Whatever library you're compiling against doesn't have that function. – Zoso Apr 11 '21 at 16:10

2 Answers2

1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zoso
  • 3,273
  • 1
  • 16
  • 27
  • Didn't solved it, however your input can be valid! Checking out more on how NDK is picking the c++ sources deeply. – мalay мeнтa Apr 12 '21 at 14:02
  • @мalayмeнтa Added another suggestion for passing a flag only for the C files. See if that helps? – Zoso Apr 12 '21 at 15:00
  • Ya that flag I only added after your suggestion there in Application.mk. Let me add the new suggestion right away! – мalay мeнтa Apr 12 '21 at 16:23
  • Something else popped up ./libfftw-3.3.8/kernel/cycle.h:559:9: error: use of undeclared identifier 'asm' https://github.com/malaythecool/Android-ImageMagick7/runs/2325770086?check_suite_focus=true – мalay мeнтa Apr 12 '21 at 16:37
  • would you like to contribute there? it's open source project. – мalay мeнтa Apr 12 '21 at 16:41
  • @мalayмeнтa Thanks for the invitation. I'll take a look at it once I've some time since it's setup appears to be pretty involved. For now, if your question is solved, you can accept this answer. For the `asm` error, you can refer to [this](https://stackoverflow.com/a/49830956/1851678) – Zoso Apr 12 '21 at 17:45
  • I guess it's reached to half build only and aborted in between, as it;s like that point didn't reached and build got aborted. I'll take a look again to start solving from there, and if that asm not appearing definitely yours is the answer. – мalay мeнтa Apr 12 '21 at 18:00
  • 1
    I saw that `memory.c`compiled [successfully](https://github.com/malaythecool/Android-ImageMagick7/runs/2325770086?check_suite_focus=true#step:4:220) in the last run. See if the `asm`change works too. Adding `-std=gnu99` should help. – Zoso Apr 12 '21 at 18:05
  • You're right, I just checked it passed memory.c! – мalay мeнтa Apr 12 '21 at 18:11
0

In case anyone else is facing this issue, bump up the minSdkVersion to 28 in your build.gradle file.

The Android NDK seems to define these functions based on minSdkVersion, even when you have -std=c11 in your CFLAGS.

File ~/Android/Sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/stdlib.h:

#if ANDROID_API >= 28
void* aligned_alloc(size_t alignment, size_t __size) __INTRODUCED_IN(28);
#endif /* __ANDROID_API >= 28 */
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dinesh
  • 449
  • 2
  • 5