Skip to content

FindThreads: TRY_RUN() invoked in cross-compiling mode

I met the problem with project configuration for cross compilation. Project code uses pthread library to find that the following is used:

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

It works with host compilation and don't for cross-compilation. Errors occurred because FindThreads.cmake:89 tries to run the executable built for cross-platform:

CMake Error: TRY_RUN() invoked in cross-compiling mode, please set the following cache variables appropriately:
   THREADS_PTHREAD_ARG (advanced)

Version of used cmake is 3.8.1.

Log of project configuration:

$ mkdir build && cd build
$ source <path_to_sdk_toolchain_env_file>
$ cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain.cmake ..
-- The C compiler identification is GNU 5.4.0
-- Check for working C compiler: /toolchain/usr/bin/unknown-linux-gcc
-- Check for working C compiler: /toolchain/usr/bin/unknown-linux-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
CMake Error: TRY_RUN() invoked in cross-compiling mode, please set the following cache variables appropriately:
   THREADS_PTHREAD_ARG (advanced)
For details see /test/build/TryRunResults.cmake
-- Check if compiler accepts -pthread - no
-- Found Threads: TRUE  
-- Configuring incomplete, errors occurred!
See also "/test/build/CMakeFiles/CMakeOutput.log".
See also "/test/build/CMakeFiles/CMakeError.log".

CMakeLists.txt

cmake_minimum_required(VERSION 3.7.2)
project(TestPthreads C)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

add_executable(${CMAKE_PROJECT_NAME} test.c)
target_link_libraries(${CMAKE_PROJECT_NAME} Threads::Threads)

Toolchain.cmake

set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSROOT $ENV{SDKSYSROOT})
set(CMAKE_FIND_ROOT_PATH $ENV{SDKSYSROOT})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

test.c - any sample using pthreads (from this http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html)

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information