简体   繁体   中英

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

In a nutshell, I can build the program with a direct g++ command line. It builds then executes successfully on the target.

If, however, I use the simplest cmake script I can think of, the build succeeds, but the program fails to execute on the target. The failed output of the executable is:

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

The g++ command line I use for a successful build and execution is:

/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

When I run the straight g++ program on the target, I get:

Atomic Success Count: 1000 of 1000.

Mutex Success Count: 1000 of 1000.

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

The CMakeLists.txt file I use is:

# 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})

I invoke cmake with:

mkdir build
cd build
cmake ..

And the cmake output is:

-- 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

Here is the VERBOSE output from the make:

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

I've exhaustively looked in StackOverflow, Google, etc. -- and the suggestions I've come across are incorporated in the CMakeLists.txt, above. But I still can't get the cmake executable to run properly on the target.

Any help would be appreciated!

I was able to resolve this problem by adding the -Wl,--no-as-needed linker option. In the CMakeLists.txt file, it's done like this:

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

Apparently the default for the gcc linker is --as-needed -- where the linker chooses not to link a library it thinks is not needed. The interesting thing is that the straight g++ command line build correctly decides that libpthread is needed. In the cmake case, g++ apparently decides that libpthread is not needed. You'll see in the make output, above, that cmake gloms together a lot of g++ command line options with duplicates and other messiness. I suspect that is probably the root issue.

Nevertheless, here is the CMakeLists.txt file that works. Notice that for my case at least, the call to FIND_PACKAGE(Threads REQUIRED) was not required at all.

# 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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM