<div dir="ltr"><div><div><div><div>I am using CMake 3.7.2 and playing around with the FindThread finder.  The following simple CMakeLists.txt works fine on various compilers: GCC, clang, MSVC (tried 2013 and 2015).<br><br><br><br>CMAKE_MINIMUM_REQUIRED( VERSION 3.1 )<br><br>ADD_EXECUTABLE( mythread main.cpp )<br>SET( THREADS_PREFER_PTHREAD_FLAG TRUE )<br>FIND_PACKAGE( Threads REQUIRED )<br>TARGET_LINK_LIBRARIES( thread Threads::Threads )<br>TARGET_COMPILE_OPTIONS( thread INTERFACE Threads::Threads )<br><br></div><br><br>However, when I attempt to cross-compile for ARM with "cmake -DCMAKE_TOOLCHAIN_FILE=blah ..", I get the following:<br><br><br><br>-- The C compiler identification is GNU 4.9.4<br>-- The CXX compiler identification is GNU 4.9.4<br>-- Check for working C compiler: /usr/bin/armv6j-hardfloat-linux-gnueabi-gcc-4.9.4<br>-- Check for working C compiler: /usr/bin/armv6j-hardfloat-linux-gnueabi-gcc-4.9.4 -- works<br>-- Detecting C compiler ABI info<br>-- Detecting C compiler ABI info - done<br>-- Detecting C compile features<br>-- Detecting C compile features - done<br>-- Check for working CXX compiler: /usr/bin/armv6j-hardfloat-linux-gnueabi-g++-4.9.4<br>-- Check for working CXX compiler: /usr/bin/armv6j-hardfloat-linux-gnueabi-g++-4.9.4 -- works<br>-- Detecting CXX compiler ABI info<br>-- Detecting CXX compiler ABI info - done<br>-- Detecting CXX compile features<br>-- Detecting CXX compile features - done<br>-- Looking for pthread.h<br>-- Looking for pthread.h - found<br>-- Looking for pthread_create<br>-- Looking for pthread_create - not found<br>-- Check if compiler accepts -pthread<br>CMake Error: TRY_RUN() invoked in cross-compiling mode, please set the following cache variables appropriately:<br>   THREADS_PTHREAD_ARG (advanced)<br>For details see /tmp/thread/build/TryRunResults.cmake<br>-- Check if compiler accepts -pthread - no<br>-- Found Threads: TRUE  <br>-- Configuring incomplete, errors occurred!<br>See also "/tmp/thread/build/CMakeFiles/CMakeOutput.log".<br>See also "/tmp/thread/build/CMakeFiles/CMakeError.log".<br><br><br></div>I can verify that the compiler does indeed accept -pthread, and IIRC might require it for pthreads to work.<br></div><br>I can see from the CMakeError.log that it might be forgetting to specify -lpthread when trying to compile the test:<br><br><br>Run Build Command:"/usr/bin/gmake" "cmTC_13694/fast"<br>/usr/bin/gmake -f CMakeFiles/cmTC_13694.dir/build.make CMakeFiles/cmTC_13694.dir/build<br>gmake[1]: Entering directory '/tmp/thread/build/CMakeFiles/CMakeTmp'<br>Building C object CMakeFiles/cmTC_13694.dir/CheckSymbolExists.c.o<br>/usr/bin/armv6j-hardfloat-linux-gnueabi-gcc-4.9.4 --sysroot=/mnt/data/chroots/pie/    -o CMakeFiles/cmTC_13694.dir/CheckSymbolExists.c.o   -c /tmp/thread/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c<br>Linking C executable cmTC_13694<br>/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_13694.dir/link.txt --verbose=1<br>/usr/bin/armv6j-hardfloat-linux-gnueabi-gcc-4.9.4 --sysroot=/mnt/data/chroots/pie/      CMakeFiles/cmTC_13694.dir/CheckSymbolExists.c.o  -o cmTC_13694 -rdynamic <br>CMakeFiles/cmTC_13694.dir/CheckSymbolExists.c.o: In function `main':<br>CheckSymbolExists.c:(.text+0x38): undefined reference to `pthread_create'<br>collect2: error: ld returned 1 exit status<br>gmake[1]: *** [CMakeFiles/cmTC_13694.dir/build.make:98: cmTC_13694] Error 1<br>gmake[1]: Leaving directory '/tmp/thread/build/CMakeFiles/CMakeTmp'<br>gmake: *** [Makefile:126: cmTC_13694/fast] Error 2<br><br>File /tmp/thread/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:<br>/* */<br>#include <pthread.h><br><br>int main(int argc, char** argv)<br>{<br>  (void)argv;<br>#ifndef pthread_create<br>  return ((int*)(&pthread_create))[argc];<br>#else<br>  (void)argc;<br>  return 0;<br>#endif<br>}<br><br><br><br><br></div>Is this a bug? Or am I not using the find correctly?<br></div>