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

时间:2016-02-18 22:27:33

标签: linux c++11 cmake pthreads cross-compiling

简而言之,我可以使用直接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

我已经详尽地查看了StackOverflow,Google等等 - 我所遇到的建议都包含在上面的CMakeLists.txt中。但我仍然无法让cmake可执行文件在目标上正常运行。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我能够通过添加-Wl,--no-as-needed链接器选项来解决此问题。在CMakeLists.txt文件中,它是这样完成的:

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

显然gcc链接器的默认值是--as-needed - 链接器选择不链接它认为不需要的库。有趣的是,正确的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})