I'm using the Android NDK and Cmake to generate shared libraries of my project.
I'm porting an existing project from Ubuntu to Android, and right now I need to port some executables files. I compile successfully all the executable file sexecpt one which needs the Threads
library.
In CMakeList.txt, there is FIND_PACKAGE(Threads)
which finds the library while compiling for Ubuntu, but not for Android.
I followed this cmake and libpthread but with no success.
I think I should write the FindThread.cmake
file but I'm pretty new to CMake and don't really know how to do it, especially as I don't know where is located the thread library for Android.
Any help would be appreciated. Thank you
Under Android, there is no need for FIND_PACKAGE(Threads)
because the Android libc standard library (called "bionic") already includes the relevant functions in threads.h
and pthread.h
. That support is not yet completely POSIX compliant and differs by API level, but will be sufficient for most cases.
So you can just skip over finding and linking to an external threads library. If your CMake file is meant for cross-platform use, it would look like this:
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
if(NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
find_package(Threads REQUIRED)
target_link_libraries (your-target-name PRIVATE Threads::Threads)
endif()
You don't need to write your own FindThread.cmake. On a standard linux installation, it can be found within /usr/share/cmake-2.8/Modules/ .
Check where this Modules/ directory might be installed on your platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With