cmake 不会使用 pthreads 构建 Linux c++11 交叉编译程序

cmake won't build a Linux c++11 cross-compiled program with pthreads

简而言之,我可以直接使用 g++ 命令行构建程序。它构建然后在目标上成功执行。

然而,如果我使用我能想到的最简单的cmake脚本,构建成功,但程序无法在目标上执行。可执行文件的失败输出是:

terminate called after throwing an instance of 'std::system_error'
what():  Enable multithreading to use std::thread: Operation not permitted
Aborted

我用于成功构建和执行的 g++ 命令行是:

/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++ -o ThreadTest -std=c++11 -pthread main.cpp ThreadTest.cpp

当我运行目标上的直接g++程序时,我得到:

Atomic Success Count: 1000 of 1000.

Mutex Success Count: 1000 of 1000.

Unclean Success Count: 922 of 1000.
Unclean Failure Count: 78 of 1000.

我使用的CMakeLists.txt文件是:

# cmake file for building the ThreadTest executable.  This requires c++11.
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)

# !!! Set up the compiler to use, first (i.e. before the PROJECT() call.)
# gcc location.
SET(CROSS_COMPILE_FULL
   "/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-")
# sysroot location
SET(CROSS_COMPILE_FULL_HOST
   "/opt/criticallink/mityomapl138_20151114/sysroots/arm926ejste-criticallink-linux-gnueabi")
SET(CMAKE_C_COMPILER "${CROSS_COMPILE_FULL}gcc")
SET(CMAKE_CXX_COMPILER "${CROSS_COMPILE_FULL}g++")
SET(CMAKE_FIND_ROOT_PATH "${CROSS_COMPILE_FULL_HOST}")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# The project will use the compiler specified above (or the default compiler for Windows.)
PROJECT(CommonSDK)

SET(MY_TEST_HOME "$ENV{TEST_HOME}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

SET(CUR_PROJECT_NAME ThreadTest)
SET(PROJECT_HOME_DIR "${MY_TEST_HOME}/${CUR_PROJECT_NAME}")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "$ENV{TEST_EXEC_HOME}")

FILE(GLOB CUR_PROJECT_FILES "${PROJECT_HOME_DIR}/*.cpp" "${PROJECT_HOME_DIR}/*.h")

FIND_PACKAGE(Threads REQUIRED)
ADD_EXECUTABLE(${CUR_PROJECT_NAME} ${CUR_PROJECT_FILES})
TARGET_LINK_LIBRARIES(${CUR_PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})

我用以下命令调用 cmake:

mkdir build
cd build
cmake ..

cmake 输出为:

-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-gcc
-- Check for working C compiler: /opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++
-- Check for working CXX compiler: /opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build

这是 make 的 VERBOSE 输出:

make VERBOSE=1

/usr/bin/cmake -H/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest -B/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
make -f CMakeFiles/ThreadTest.dir/build.make CMakeFiles/ThreadTest.dir/depend
make[2]: Entering directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
cd /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/ThreadTest.dir/DependInfo.cmake --color=
Dependee "/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/ThreadTest.dir/DependInfo.cmake" is newer than depender "/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/ThreadTest.dir/depend.internal".
Dependee "/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles/ThreadTest.dir/depend.internal".
Scanning dependencies of target ThreadTest
make[2]: Leaving directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
make -f CMakeFiles/ThreadTest.dir/build.make CMakeFiles/ThreadTest.dir/build
make[2]: Entering directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
/usr/bin/cmake -E cmake_progress_report /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles 1
[ 50%] Building CXX object CMakeFiles/ThreadTest.dir/main.cpp.o
/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++    -O2 -pipe -g -feliminate-unused-debug-types  -std=c++11 -pthread   -o CMakeFiles/ThreadTest.dir/main.cpp.o -c /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/main.cpp
/usr/bin/cmake -E cmake_progress_report /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles 2
[100%] Building CXX object CMakeFiles/ThreadTest.dir/ThreadTest.cpp.o
/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++    -O2 -pipe -g -feliminate-unused-debug-types  -std=c++11 -pthread   -o CMakeFiles/ThreadTest.dir/ThreadTest.cpp.o -c /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/ThreadTest.cpp
Linking CXX executable /home/mitydsp/Code/CommonSDK/OutputTest/DbricHost/ThreadTest
/usr/bin/cmake -E cmake_link_script CMakeFiles/ThreadTest.dir/link.txt --verbose=1
/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-g++    -O2 -pipe -g -feliminate-unused-debug-types  -std=c++11 -pthread   -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed CMakeFiles/ThreadTest.dir/main.cpp.o CMakeFiles/ThreadTest.dir/ThreadTest.cpp.o  -o /home/mitydsp/Code/CommonSDK/OutputTest/DbricHost/ThreadTest -rdynamic -lpthread 
make[2]: Leaving directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
/usr/bin/cmake -E cmake_progress_report /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles  1 2
[100%] Built target ThreadTest
make[1]: Leaving directory `/home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build'
/usr/bin/cmake -E cmake_progress_start /home/mitydsp/Code/CommonSDK/source/Tests/ThreadTest/build/CMakeFiles 0

我详尽地查看了 Whosebug、Google 等——我遇到的建议已纳入上面的 CMakeLists.txt。但是我仍然无法在目标上正确地获取 cmake 可执行文件 运行。

如有任何帮助,我们将不胜感激!

我通过添加 -Wl,--no-as-needed linker 选项解决了这个问题。在CMakeLists.txt文件中,是这样写的:

SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-as-needed ${THREADTEST_COMPILE_FLAG}")

显然 gcc linker 的默认值是 --as-needed —— linker 选择不 link 它认为不需要的库。有趣的是,直接的 g++ 命令行构建正确地决定了需要 libpthread。在 cmake 的情况下,g++ 显然决定不需要 libpthread。您会在上面的 make 输出中看到,cmake 将大量带有重复和其他混乱的 g++ 命令行选项混在一起。我怀疑这可能是根本问题。

不过,CMakeLists.txt 文件仍然有效。请注意,至少就我而言,根本不需要调用 FIND_PACKAGE(Threads REQUIRED)

# cmake file for building the ThreadTest executable.  This requires c++11.
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)

# !!! Set up the compiler to use, first (i.e. before the PROJECT() call.)
# gcc location.
SET(CROSS_COMPILE_FULL
   "/opt/criticallink/mityomapl138_20151114/sysroots/i686-clsdk-linux/usr/bin/arm-criticallink-linux-gnueabi/arm-criticallink-linux-gnueabi-")
# sysroot location
SET(CROSS_COMPILE_FULL_HOST
   "/opt/criticallink/mityomapl138_20151114/sysroots/arm926ejste-criticallink-linux-gnueabi")
SET(CMAKE_C_COMPILER "${CROSS_COMPILE_FULL}gcc")
SET(CMAKE_CXX_COMPILER "${CROSS_COMPILE_FULL}g++")
SET(CMAKE_FIND_ROOT_PATH "${CROSS_COMPILE_FULL_HOST}")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

# The project will use the compiler specified above (or the default compiler for Windows.)
PROJECT(CommonSDK)

SET(MY_TEST_HOME "$ENV{TEST_HOME}")
SET(THREADTEST_COMPILE_FLAG "-std=c++11 -pthread")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${THREADTEST_COMPILE_FLAG}")
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-as-needed ${THREADTEST_COMPILE_FLAG}")

SET(CUR_PROJECT_NAME ThreadTest)
SET(PROJECT_HOME_DIR "${MY_TEST_HOME}/${CUR_PROJECT_NAME}")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "$ENV{TEST_EXEC_HOME}")

FILE(GLOB CUR_PROJECT_FILES "${PROJECT_HOME_DIR}/*.cpp" "${PROJECT_HOME_DIR}/*.h")

ADD_EXECUTABLE(${CUR_PROJECT_NAME} ${CUR_PROJECT_FILES})
TARGET_LINK_LIBRARIES(${CUR_PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})