CMake 无法使用 Linaro 工具链找到线程

CMake can't find threads with Linaro toolchain

我无法让 CMake 使用 Linaro ARM 工具链查找线程(我尝试了几种不同的工具链)。这是我所做的:

我创建了一个 Toolchain-Linaro-arm.cmake 文件,如下所示:

set (CMAKE_SYSTEM_NAME Linux)                                                                           

include (CMakeForceCompiler)                                                                            
set (TOOLCHAIN_BASE "/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf/")                          

set (CMAKE_SYSTEM_PROCESSOR armhf-cortexa9)                                                             

CMAKE_FORCE_C_COMPILER("${TOOLCHAIN_BASE}/bin/arm-linux-gnueabihf-gcc" GNU)                             
CMAKE_FORCE_CXX_COMPILER("${TOOLCHAIN_BASE}/bin/arm-linux-gnueabihf-g++" GNU)                           

set (CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} /home/user/sysroot)                                   

set (CMAKE_SIZEOF_VOID_P 4)                                                                             

set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)                                                           
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)                                                            
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)                                                            
set (CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

我创建了一个使用线程的最小示例项目:

~/threadstest $ ls
CMakeLists.txt main.cpp

~/threadstest $ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(threads_test)
add_executable(test main.cpp)
find_package(Threads REQUIRED)
target_link_libraries(test, ${CMAKE_THREAD_LIBS_INIT})

~/threadstest $ cat main.cpp
int main() { }

~/threadstest $ mkdir build; cd build

~/threadstest/build $ cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Linaro-arm.cmake ..
CMake Error at /opt/cmake-3.3.1-Linux-x86_64/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
  /opt/cmake-3.3.1-Linux-x86_64/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
  /opt/cmake-3.3.1-Linux-x86_64/share/cmake-3.3/Modules/FindThreads.cmake:205 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:8 (find_package)

~/threadstest/build $ cat CMakeFiles/CMakeError.log

Determining if files pthread.h exist failed with the following output:
Change Dir: /home/user/threadstest/arm/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_37a3c/fast"
/usr/bin/make -f CMakeFiles/cmTC_37a3c.dir/build.make CMakeFiles/cmTC_37a3c.dir/build
make[1]: Entering directory `/home/user/threadstest/arm/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_37a3c.dir/CheckIncludeFiles.c.o
/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc    -o CMakeFiles/cmTC_37a3c.dir/CheckIncludeFiles.c.o   -c /home/user/threadstest/arm/CMakeFiles/CMakeTmp/CheckIncludeFiles.c
/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.14' not found (required by /opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc)
make[1]: Leaving directory `/home/user/threadstest/arm/CMakeFiles/CMakeTmp'
make[1]: *** [CMakeFiles/cmTC_37a3c.dir/CheckIncludeFiles.c.o] Error 1
make: *** [cmTC_37a3c/fast] Error 2

Source:
/* */
#include <pthread.h>


int main(void){return 0;}

我知道存在 pthread 库:

$ find ~/sysroot -name "*pthread*"
/home/user/sysroot/usr/lib/libpthread_nonshared.a
/home/user/sysroot/usr/lib/libpthread.so.0
/home/user/sysroot/usr/lib/libpthread-2.19-2014.08-1-git.so
/home/user/sysroot/usr/lib/libpthread_p.a
/home/user/sysroot/usr/lib/libpthread.a
/home/user/sysroot/usr/lib/libpthread.so
/home/user/sysroot/usr/include/bits/pthreadtypes.h
/home/user/sysroot/usr/include/pthread.h

看起来有些东西还在看我的原生 libc 而不是交叉编译环境:

/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc -o CMakeFiles/cmTC_37a3c.dir/CheckIncludeFiles.c.o -c /home/user/threadstest/arm/CMakeFiles/CMakeTmp/CheckIncludeFiles.c /opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc: /lib/x86_64-linux-gnu/libc.so.6: version "GLIBC_2.14" not found (required by /opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc)

问题根本原因不在pthread库中:

根本原因是:

/lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.14' not found (required by /opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc)

您的编译器正在尝试使用本地 libc

1) 在下载的 sysroot 中找到 libc.so* 并使用以下命令检查版本:

objdump -p libc.so.6  | grep "Version References:" -A 10

如果一切正常,请使用此库(带有 --sysroot 选项);

您可以尝试手动操作:

/opt/gcc-linaro-4.9-2015.05-x86_64_arm-linux-gnueabihf//bin/arm-linux-gnueabihf-gcc -o CMakeFiles/cmTC_37a3c.dir/CheckIncludeFiles.c.o -c /home/user/threadstest/arm/CMakeFiles/CMakeTmp/CheckIncludeFiles.c --sysroot /home/user/sysroot/

2) 你的工具链可能编译的 libc.so 版本与你系统上安装的版本不同,在你的系统上下载合适的版本并使用它。