Search code examples
androidc++boostbjam

How to build boost as shared libraries for Android


I successfully compiled boost 1.70 for Android armeabiv7a with NDK r21b.

I used user-config.jam:

import os ;
local AndroidNDKRoot = [ os.environ ANDROIDNDKROOT ] ;
local AndroidBinariesPath = [ os.environ CLANGPATH ] ;
local TargetPlatform = [ os.environ TARGET ] ;

using clang : armeabiv7a
:
$(AndroidBinariesPath)/clang++
:
<archiver>$(AndroidBinariesPath)/llvm-ar
<ranlib>$(AndroidBinariesPath)/llvm-ranlib
<compileflags>-fexceptions
<compileflags>-frtti
<compileflags>-ffunction-sections
<compileflags>-funwind-tables
<compileflags>-fstack-protector-strong
<compileflags>-Wno-invalid-command-line-argument
<compileflags>-Wno-unused-command-line-argument
<compileflags>-no-canonical-prefixes
<compileflags>-I$(AndroidNDKRoot)/sources/cxx-stl/llvm-libc++/include
<compileflags>-I$(AndroidNDKRoot)/sources/cxx-stl/llvm-libc++abi/include
<compileflags>-I$(AndroidNDKRoot)/sources/android/support/include
<compileflags>-DANDROID
<compileflags>-Wa,--noexecstack
<compileflags>-Wformat
<compileflags>-Werror=format-security
<compileflags>-DNDEBUG
<compileflags>-O2
<compileflags>$(AndroidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(PlatformOS)-x86_64
<compileflags>-target
<compileflags>armv7a-linux-androideabi21
<compileflags>-march=armv7-a
<compileflags>-mfloat-abi=softfp
<compileflags>-mfpu=vfpv3-d16
<compileflags>-mthumb
<compileflags>-fPIC
<compileflags>-fno-integrated-as
<compileflags>--sysroot
<compileflags>$(AndroidNDKRoot)/sysroot
<compileflags>-isystem
<compileflags>$(AndroidNDKRoot)/sysroot/usr/include/arm-linux-androideabi
<compileflags>-D__ANDROID_API__=21
;

and command line:

set ANDROIDNDKROOT=C:\Android\android_sdk\ndk-bundle
set NDKVER=r21b
set CLANGPATH=%ANDROIDNDKROOT%\toolchains\llvm\prebuilt\windows-x86_64\bin
set PATH=%PATH%;%CLANGPATH%
bjam -q link=static --without-math --without-context --without-coroutine --without-fiber --without-python --build-type=minimal architecture=arm --ignore-site-config -j8 target-os=android toolset=clang-armeabiv7a threading=multi --layout=tagged --build-dir=build_static_armeabiv7a/%NDKVER% --stagedir=stage_static_armeabiv7a_%NDKVER% stage

This works perfectly.

Now, I'd like to build the libraries as shared, so I run:

bjam -q link=shared --without-math --without-context --without-coroutine --without-fiber --without-python --build-type=minimal architecture=arm --ignore-site-config -j8 target-os=android toolset=clang-armeabiv7a threading=multi --layout=tagged --build-dir=build_shared_armeabiv7a/%NDKVER% --stagedir=stage_shared_armeabiv7a_%NDKVER% stage

But then I get the following error:

clang-linux.link.dll build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\libboost_atomic-mt-a32.so.1.70.0
clang++: error: unable to execute command: program not executable
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

    "C:\Android\android_sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin/clang++"   -o "build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\libboost_atomic-mt-a32.so.1.70.0" -Wl,-soname -Wl,libboost_atomic-mt-a32.so.1.70.0 -shared -Wl,--start-group "build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\lockpool.o"  -Wl,-Bstatic  -Wl,-Bdynamic  -Wl,--end-group -fPIC -fvisibility=hidden -fvisibility-inlines-hidden

...failed clang-linux.link.dll build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\libboost_atomic-mt-a32.so.1.70.0...

I tried to manually execute the failing command with -v to get more detail:

"C:\Android\android_sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin/clang++"   -o "build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\libboost_atomic-mt-a32.so.1.70.0" -Wl,-soname -Wl,libboost_atomic-mt-a32.so.1.70.0 -shared -Wl,--start-group "build_shared_armeabiv7a\r21b\boost\bin.v2\libs\atomic\build\clang-linux-armeabiv7a\release\target-os-android\threading-multi\visibility-hidden\lockpool.o" -Wl,-Bdynamic  -Wl,--end-group -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -v

Error reported is then:

"ld" -m i386pep --shared -Bdynamic -e DllMainCRTStartup --enable-auto-image-base -o "build_shared_armeabiv7a\\r21b\\boost\\bin.v2\\libs\\atomic\\build\\clang-linux-armeabiv7a\\release\\target-os-android\\threading-multi\\visibility-hidden\\libboost_atomic-mt-a32.so.1.70.0" dllcrt2.o crtbegin.o "-LC:\\Android\\r21a_Qt5_14\\android_sdk\\ndk-bundle\\toolchains\\llvm\\prebuilt\\windows-x86_64\\x86_64-w64-mingw32\\lib" "-LC:\\Android\\r21a_Qt5_14\\android_sdk\\ndk-bundle\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib" "-LC:\\Android\\r21a_Qt5_14\\android_sdk\\ndk-bundle\\toolchains\\llvm\\prebuilt\\windows-x86_64\\x86_64-w64-mingw32/sys-root/mingw/lib" -soname libboost_atomic-mt-a32.so.1.70.0 --start-group "build_shared_armeabiv7a\\r21b\\boost\\bin.v2\\libs\\atomic\\build\\clang-linux-armeabiv7a\\release\\target-os-android\\threading-multi\\visibility-hidden\\lockpool.o" -Bdynamic --end-group -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt crtend.o
clang++: error: unable to execute command: program not executable
clang++: error: linker command failed with exit code 1 (use -v to see invocation)

Looks like "ld" is not found, I suppose I should tell the build system when "ld" is, but I could not find any in C:\Android\android_sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin nor how to specify a "ld" path in user-config.jam.

Any help would be appreciated!


Solution

  • By looking where a "ld.exe" was present in C:\Android\android_sdk\ndk-bundle\toolchains\llvm folder, I found some under C:\Android\r21a_Qt5_14\android_sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\<some platform name>\bin so I concluded that target platform was probably missing.

    I added:

    <linkflags>-target
    <linkflags>armv7a-linux-androideabi21
    

    in user-config.jam and I could compile boost as shared library.