Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake : FIND_PACKAGE(Threads) for Android cross-compilation

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

like image 866
leochab Avatar asked May 13 '11 10:05

leochab


2 Answers

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()
like image 118
tanius Avatar answered Oct 18 '22 20:10

tanius


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.

like image 27
augustin Avatar answered Oct 18 '22 22:10

augustin