find_package(Threads) leads to CMake Error: TRY_RUN() invoked in cross-compiling mode
I tried to fix linux (p)thread usage on a proprietary, somewhat
complex (300-400 cmake files, ca 30.000 lines) cmake project. We
have CMAKE_TOOLCHAIN_FILEs for the cross compiling platforms.
These set(CMAKE_SYSTEM_NAME Linux),
CMAKE_C_FLAGS(....-D_REENTRANT... CACHE STRING "" FORCE)) and so
I like to see -pthread on gcc (g++4 and g++5) and if needed
-lphtread. I now spent almost two days without success and hope I
can get a bit help here.
I learned that someone is supposed to use
instead of specifying CMAKE_C_FLAGS. This works fine for a
minimal CMakeLists.txt example (cmake version 3.6.2):
project(phello C CXX)
-- 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:
For details see
-- Check if compiler accepts -pthread - no
-- Found Threads: TRUE
-- Configuring incomplete, errors occurred!
See also "/local/users/sdettmer/work/cmake-toolchain-1.4/threadtest/b2/CMakeFiles/CMakeOutput.log".
See also "/local/users/sdettmer/work/cmake-toolchain-1.4/threadtest/b2/CMakeFiles/CMakeError.log".
and then no Makefile as expected:
make: *** No targets specified and no makefile found. Stop.
If then I run cmake again without any change:
-- Configuring done
-- Generating done
-- Build files have been written to:
a behavior I don't understand. I think this is incorrect. I think,
first, cmake should not TRY_RUN when crosscompiling,
second, running cmake again should not generate different results.
What do I wrong?
How can I get it working correctly on first run?
In CMakeFiles/CMakeError.log I see that linking fails without
-pthread (undefined reference to `pthread_create') and then works
when using -pthread, so actually looks like expected when
assuming find_package(Threads) tries to check first whether
-pthread is needed at all and then if it is working. However, the
second run may treated as error:
Determining if compiler accepts -pthread returned
PLEASE_FILL_OUT-FAILED_TO_RUN instead of 2. The compiler had the
Change Dir: /local/users/sdettmer/work/cmake-toolchain-1.4/threadtest/b2/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_47cd8/fast"
/usr/bin/make -f CMakeFiles/cmTC_47cd8.dir/build.make
make: Entering directory
Building C object CMakeFiles/cmTC_47cd8.dir/CheckForPthreads.c.o
-o CMakeFiles/cmTC_47cd8.dir/CheckForPthreads.c.o -c
Linking C executable cmTC_47cd8
/usr/local/bin/cmake -E cmake_link_script
If I copy the mentioned gcc command to a shell and change only
the file name (CheckForPthreads does not exist anymore, so I use
an own pthread_create() main test), it works as expected, no
output to console but
$ file cmTC_47cd8
cmTC_47cd8: ELF 32-bit MSB executable, PowerPC or cisco 4500,
version 1 (SYSV), dynamically linked,
interpreter /lib/ld.so.1, for GNU/Linux 2.6.10, not stripped
so compiler seems to work fine.
What does "-pthread returned PLEASE_FILL_OUT-FAILED_TO_RUN
instead of 2." mean? That I'm supposed to run the test binary on
target and put the result in a CMakeFiles.txt variable?
Any hints appreciated,
# This file was generated by CMake because it detected TRY_RUN() commands
# in crosscompiling mode. It will be overwritten by the next CMake run.
# Copy it to a safe location, set the variables to appropriate values
# and use it then to preset the CMake cache (using -C).
# indicates whether the executable would have been able to run on its
# target platform. If so, set THREADS_PTHREAD_ARG to
# the exit code (in many cases 0 for success), otherwise enter
# The THREADS_HAVE_PTHREAD_ARG variable holds the build result for
# Source file : /usr/local/share/cmake-3.6/Modules/CheckForPthreads.c
# Executable :
# Run arguments :
# Called from:  /usr/local/share/cmake-3.6/Modules/FindThreads.cmake
#  /usr/local/share/cmake-3.6/Modules/FindThreads.cmake
CACHE STRING "Result from TRY_RUN" FORCE)
Re: find_package(Threads) leads to CMake Error: TRY_RUN() invoked in cross-compiling mode
a small correction:
On Thu, Aug 3, 2017 at 5:59 PM, Steffen Dettmer
<[hidden email]> wrote:
> Building C object CMakeFiles/cmTC_47cd8.dir/CheckForPthreads.c.o
> /opt/xyzcross/x86-linux2/bin/powerpc-linux-gnu-gcc --sysroot=...
> -o CMakeFiles/cmTC_47cd8.dir/CheckForPthreads.c.o -c
> If I copy the mentioned gcc command to a shell and change only
> the file name (CheckForPthreads does not exist anymore
Ohh, of course the .c file still exists, just the directory for
the .o file had been removed. When I create it, the compile and
link commands from the ErrorLog both work fine.