Android Studio 2.2.2 NDK and Boost Libraries problem

1,328 views
Skip to first unread message

Robert Black

unread,
Dec 12, 2016, 3:30:33 PM12/12/16
to android-ndk

Hi all,


I am fairly new to android studio and am trying to get to grips with the cradle and CMake features so I can make use of my c++ project source code.


I have found similar questions, however, non of them are related to the new Android Studio 2.2.2 using CMake without the need for Android.mk files, etc...


This code relies on Boost::asio, Boost::thread and Boost::sharedPointers which all depend on the Boost::system library for errors etc.  I have managed to get the Boost headers working but am totally stumped after pouring through documentation for a week now and would really appreciate it if anyone could throw some light on this before I loose all my hair...


My development environment is Unix based Mac OS X


The CMakeLists.txt file is as below: 


# Sets the minimum version of CMake required to build the native

# library. You should either keep the default value or only pass a

# value of 3.4.0 or lower.


cmake_minimum_required(VERSION 3.4.1)


include_directories(/usr/local/Cellar/jpeg/8d/include)

add_library(tiff_lib SHARED IMPORTED)

set_target_properties(tiff_lib PROPERTIES IMPORTED_LOCATION

    /usr/local/Cellar/libtiff/4.0.6/lib/libtiff.a)


include_directories(/usr/local/Cellar/libpng/1.6.21/include)

add_library(png_lib SHARED IMPORTED)

set_target_properties(png_lib PROPERTIES IMPORTED_LOCATION

    /usr/local/Cellar/libpng/1.6.21/lib/libpng.a)


include_directories(/usr/local/Cellar/libtiff/4.0.6/include)

add_library(jpeg_lib SHARED IMPORTED)

set_target_properties(jpeg_lib PROPERTIES IMPORTED_LOCATION

    /usr/local/Cellar/jpeg/8d/lib/libjpeg.a)


set (BOOST_ROOT "/usr/local/Cellar/boost/1.62.0")

set (Boost_INCLUDE_DIR "/usr/local/Cellar/boost/1.62.0/include")

set (BOOST_LIBRARYDIR "/usr/local/Cellar/boost/1.62.0/lib")

set (Boost_USE_STATIC_LIBS ON)


include_directories(SYSTEM ${Boost_INCLUDE_DIR})


find_package(Boost 1.62.0 EXACT COMPONENTS

                date_time

            filesystem

            regex

            serialization

            system

            timer)


include_directories(/Users/robertblack/Desktop/Member_Aandroid_1/app/src/main/cpp/include)


add_subdirectory(src/main/cpp/utilities)


# Creates and names a library, sets it as either STATIC

# or SHARED, and provides the relative paths to its source code.

# You can define multiple libraries, and CMake builds it for you.

# Gradle automatically packages shared libraries with your APK.


add_library( # Sets the name of the library.

             native-lib


             # Sets the library as a shared library.

             SHARED


             # Provides a relative path to your source file(s).

             # Associated headers in the same location as their source

             # file are automatically included.

             src/main/cpp/native-lib.cpp)


# Searches for a specified prebuilt library and stores the path as a

# variable. Because system libraries are included in the search path by

# default, you only need to specify the name of the public NDK library

# you want to add. CMake verifies that the library exists before

# completing its build.


find_library( # Sets the name of the path variable.

              log-lib


              # Specifies the name of the NDK library that

              # you want CMake to locate.

              log )


# Specifies libraries CMake should link to your target library. You

# can link multiple libraries, such as libraries you define in the

# build script, prebuilt third-party libraries, or system libraries.


target_link_libraries( # Specifies the target library.

                       native-lib


                       mathUtilities

                       stringUtilites


                       # Links the target library to the log library

                       # included in the NDK.

                       ${log-lib} )


And the cradle.build file is as follows:


apply plugin: 'com.android.application'


android {

    compileSdkVersion 25

    buildToolsVersion "25.0.1"

    defaultConfig {

        applicationId "com.example.robertblack.member_aandroid_1"

        minSdkVersion 11

        targetSdkVersion 25

        versionCode 1

        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild {

            cmake {

                cppFlags "-std=c++11 -frtti -fexceptions -DBOOST_SYSTEM_NO_DEPRECATED"

            }

        }

    }

    buildTypes {

        release {

            minifyEnabled false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        }

    }

    externalNativeBuild {

        cmake {

            path "CMakeLists.txt"

        }

    }

}


dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {

        exclude group: 'com.android.support', module: 'support-annotations'

    })

    compile 'com.android.support:appcompat-v7:25.0.1'

    compile 'com.android.support:design:25.0.1'

    testCompile 'junit:junit:4.12'

}


And the errors I am receiving when trying to build are as follows:


Error:FAILURE: Build failed with an exception.


* What went wrong:

Execution failed for task ':app:externalNativeBuildDebug'.

> Build command failed.

  Error while executing '/Applications/android-sdk-macosx/cmake/3.6.3155560/bin/cmake' with arguments {--build /Users/robertblack/Desktop/Member_Aandroid_1/app/.externalNativeBuild/cmake/debug/mips64 --target workerThreadImp}

  [1/2] Building CXX object src/main/cpp/utilities/Assets/CMakeFiles/workerThreadImp.dir/WorkerThreadImp.cpp.o

  [2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/mips64/libworkerThreadImp.so

  FAILED: : && /Applications/android-sdk-macosx/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  -target mips64el-none-linux-android -gcc-toolchain /Applications/android-sdk-macosx/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Applications/android-sdk-macosx/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -std=c++11 -frtti -fexceptions -DBOOST_SYSTEM_NO_DEPRECATED -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libworkerThreadImp.so -o ../../../../build/intermediates/cmake/debug/obj/mips64/libworkerThreadImp.so src/main/cpp/utilities/Assets/CMakeFiles/workerThreadImp.dir/WorkerThreadImp.cpp.o  -lm "/Applications/android-sdk-macosx/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && :

  src/main/cpp/utilities/Assets/CMakeFiles/workerThreadImp.dir/WorkerThreadImp.cpp.o: In function `boost::asio::error::get_system_category()':

  /usr/local/Cellar/boost/1.62.0/include/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'

  clang++: error: linker command failed with exit code 1 (use -v to see invocation)

  ninja: build stopped: subcommand failed.



* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.



Thank you for your time to look at this and I look forward to you replies.

Kenneth Geisshirt

unread,
Dec 12, 2016, 5:14:37 PM12/12/16
to android-ndk
Looks like you are trying to use libraries from Home Brew in your Android app. You cannot use binary files compiled for OS X. You have to add the _source code_ of the libraries to your Android project and compile them together with your app.

- Kenneth

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/e643cc44-bd8a-42a7-8ccf-13c07569cfcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Kenneth Geisshirt, M.Sc., Ph.D.
Majbøl Allé 18, DK-2770 Kastrup, +45 60 62 71 82

AppCoder

unread,
Dec 13, 2016, 1:37:09 PM12/13/16
to android-ndk, ken...@geisshirt.dk
I think there used to be some attempts to make Boost build with cmake, but they currently appear to be out of date (4+ years).   The native boost build system is bjam.  The easiest way I've seen to make it work is by building a standalone tool chain with the NDK and having config.bjam point at that.   I've only done the arm and arm64 stuff (I suspect mips would be similar.)  bjam does a lot of config magic for you that would be painful to replicate and keep up to date with cmake.

I would suggest making standalone ndk compilers for the ABIs and OS levels you want, making bjam files to build them, and invoking bjam with --build-dir putting them somewhere you can find them (should include the ABI in the dir name) and then pointing your cmake at them.

Note that you are soon to have the same problem with libtiff.a and libpng.a (I don't know how well either of those work with cmake.)


      Dan S.

Dan Albert

unread,
Dec 14, 2016, 5:55:57 PM12/14/16
to android-ndk, dan.s...@gmail.com, ken...@geisshirt.dk
Having an example of how to get boost working with the NDK would be great. Would be a good contribution for http://stackoverflow.com/documentation/android-ndk/topics :) I'd be happy to push for getting something like that into the official docs as well ("So you need to port something to the NDK...").

To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk+unsubscribe@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

javacom

unread,
Dec 15, 2016, 6:24:57 PM12/15/16
to andro...@googlegroups.com

Jayasekaran R

unread,
Dec 15, 2016, 6:25:07 PM12/15/16
to android-ndk, ken...@geisshirt.dk
I am new to android studio. I created a simple project and run it. I am getting the below issue.

Gradle 'MyApplication' project refresh failed
Error:Cause: unable to find valid certification path to requested target

Can any one  please help me to solve the issue.?

Regards,
Jayasekaran

ashis...@thesynapses.com

unread,
Dec 11, 2018, 2:25:14 AM12/11/18
to android-ndk
So what is the solution? I am also getting the same issue.
app\src\main\cpp/main.cpp:81: error: undefined reference to 'init()'

app\src\main\cpp/main.cpp:127: error: undefined reference to 'Player::getInstance(int, int, bool, int, int, int, int, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, int, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >)'

app\src\main\cpp/main.cpp:128: error: undefined reference to 'Player::play()'
app\src\main\cpp/main.cpp:131: error: undefined reference to 'Player::~Player()'

----------*-----------
This email and any attachments are confidential and intended only for the individual(s) named. If you are not the named addressee or have received this email by mistake, please (a) notify the sender, and (b) delete this email immediately without distributing or retaining.
The sender does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission.


Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the organization. Our employees, contractors & students are expressly required not to infringe on copyright or any other law. Any such communication is contrary to company policy and outside the scope of his/her engagement with the company,will not accept any liability in respect of such communication, and he/she will be personally responsible and liable for any damages.

Andrew Esh

unread,
Dec 11, 2018, 9:18:25 AM12/11/18
to android-ndk
We do not use boost on Android, but we target ARM platforms. (We do not target mips64, as you do.) We are trying to standardize on clang for our code base because most of our code does compile for Android, and Android NDK is standardizing on clang. We also target other ARM based platforms (Raspberry Pi), and we compile boost for them.
What we ran into is a compatibility problem between boost 1_65_1 and clang, so we went back to using gcc to do our boost build. Here are the steps we use to have boost build with gcc instead of clang:

      ./bootstrap.sh
      TOOLSET=""
      if [ x$TARGET_MACHINE != "x" ] ; then
          sed -i -e "s/using gcc ;/using gcc : arm : $TARGET_MACHINE-g++ ;/" project-config.jam
          TOOLSET="toolset=gcc-arm"
      fi
      ./bjam release ${TOOLSET} link=shared --prefix=$SYSROOT --stagedir=$SYSROOT --exec-prefix=$SYSROOT --include-dir=$SYSROOT/include --libdir=$SYSROOT/lib -d+2 --with-iostreams --with-filesystem --with-date_time --with-thread --with-test --with-system
      ./bjam install ${TOOLSET} link=shared --prefix=$SYSROOT --stagedir=$SYSROOT --exec-prefix=$SYSROOT --include-dir=$SYSROOT/include --libdir=$SYSROOT/lib -d+2 --with-iostreams --with-filesystem --with-date_time --with-thread --with-test --with-system

Perhaps you need to try using gcc.

Also: Why use such an old version of the Android tools (25.0.1)? Maybe upgrade to 28.0.3. The boost library version is also very old.

Ashish Jain

unread,
Dec 12, 2018, 1:00:02 AM12/12/18
to andro...@googlegroups.com
Thanks to reply.

But I have also tried with Boost 1.68 version with NDK (r18) but still getting below errors:

FAILED: cmd.exe /C "cd . && D:\Software\Latest\AndroidSdk\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=armv7-none-linux-androideabi16 --gcc-toolchain=D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sysroot -fPIC -isystem D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11  -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/platforms/android-16/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -LD:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/main.cpp.o  -llog 

C:/Users/sai/Downloads/boost_1.68.0_prebuilt_ndk_18b/1.68.0/libs/llvm/armeabi-v7a/libboost_system.so C:/Users/sai/Downloads/boost_1.68.0_prebuilt_ndk_18b/1.68.0/libs/llvm/armeabi-v7a/libboost_chrono.so -latomic -lm "D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libandroid_support.a" "D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a" "D:/Software/Latest/AndroidSdk/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so" && cd ."

C:\Users\sai\Downloads\Boost-for-Android-master\Boost-for-Android-master\example_app\app\src\main\cpp/main.cpp:54: error: undefined reference to 'Player::getInstance()'

C:\Users\sai\Downloads\Boost-for-Android-master\Boost-for-Android-master\example_app\app\src\main\cpp/main.cpp:56: error: undefined reference to 'Player::~Player()'

C:/Users/sai/Downloads/boost_1.68.0_prebuilt_ndk_18b/1.68.0/include\boost/log/sources/basic_logger.hpp:456: error: undefined reference to 
'boost::log::v2s_mt_posix::core::get_logging_enabled() const'

C:/Users/sai/Downloads/boost_1.68.0_prebuilt_ndk_18b/1.68.0/include\boost/log/sources/record_ostream.hpp:526: error: undefined reference to 'boost::log::v2s_mt_posix::aux::unhandled_exception_count()' etc.

C:/Users/sai/Downloads/boost_1.68.0_prebuilt_ndk_18b/1.68.0/include\boost/log/utility/setup/common_attributes.hpp:53: error: undefined reference to 'boost::log::v2s_mt_posix::core::get()'

I am using prebuilt lib. See the attachment.
Also Is it possible to use Boost 1.59 version with latest NDK(18) version?


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

For more options, visit https://groups.google.com/d/optout.
boost1.PNG
libs.PNG
Reply all
Reply to author
Forward
0 new messages