【发布时间】:2021-11-12 21:55:00
【问题描述】:
我正在尝试使用几个编译器版本来运行asan 来捕获不同目标的问题。为此,我使用自定义 cmake 工具链文件,并首先使用官方 GCC Docker 容器。使用gcc:11 一切正常,但对于gcc:10 和gcc:9,我根本无法使用此自定义工具链文件链接pthread。我已经确认我可以在没有自定义工具链文件的情况下链接pthread,并且对于ubsan 工具链文件一切正常。我怀疑我遗漏了一些小细节。我有一个最小的例子可以可靠地重现这一点:
# asan.toolchain.cmake
set (CMAKE_C_COMPILER gcc)
set (CMAKE_CXX_COMPILER g++)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS_INIT "-O0 -g -fsanitize=address -fno-omit-frame-pointer -fno-var-tracking-assignments")
set (CMAKE_LINKER_FLAGS_INIT "-fsanitize=address -fno-omit-frame-pointer")
set (CMAKE_BUILD_TYPE Debug)
# CMakeLists.txt
cmake_minimum_required (VERSION 3.13...3.18)
project (ASAN_TEST LANGUAGES CXX)
set (CMAKE_THREAD_PREFER_PTHREADS ON)
set (THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package (Threads REQUIRED)
add_executable (test main.cpp)
target_link_libraries (test PRIVATE Threads::Threads)
// main.cpp
#include <pthread.h>
int main() {
pthread_key_t key{};
pthread_setspecific(key, nullptr);
}
使用docker run -itv $(pwd):/usr/src gcc:9 在docker 中安装项目并调用cmake 和make 会产生以下结果:
root@94f4b543dd0b: apt update && apt install -y cmake
root@94f4b543dd0b: mkdir build && cd build
root@94f4b543dd0b:/build# cmake -DCMAKE_TOOLCHAIN_FILE=/usr/src/asan.toolchain.cmake /usr/src/
-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/local/bin/g++
-- Check for working CXX compiler: /usr/local/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /build
root@87d5ef6a3b5f:/build# make VERBOSE=1
/usr/bin/cmake -S/usr/src -B/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /build/CMakeFiles /build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/build'
cd /build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /usr/src /usr/src /build /build /build/CMakeFiles/test.dir/DependInfo.cmake --color=
Dependee "/build/CMakeFiles/test.dir/DependInfo.cmake" is newer than depender "/build/CMakeFiles/test.dir/depend.internal".
Dependee "/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/build/CMakeFiles/test.dir/depend.internal".
Scanning dependencies of target test
make[2]: Leaving directory '/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/build'
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
/usr/local/bin/g++ -O0 -g -fsanitize=address -fno-omit-frame-pointer -fno-var-tracking-assignments -g -std=gnu++14 -o CMakeFiles/test.dir/main.cpp.o -c /usr/src/main.cpp
[100%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/local/bin/g++ -O0 -g -fsanitize=address -fno-omit-frame-pointer -fno-var-tracking-assignments -g CMakeFiles/test.dir/main.cpp.o -o test
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: undefined reference to symbol 'pthread_setspecific@@GLIBC_2.2.5'
/usr/bin/ld: //lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:84: test] Error 1
make[2]: Leaving directory '/build'
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/build'
make: *** [Makefile:84: all] Error 2
如果使用ubsan 工具链文件或没有任何工具链文件调用cmake,则编译和链接。请注意,在 make 调用的输出中,-pthread 缺失,尽管 cmake 报告它已找到。
为了完整起见,这里是ubsan 工具链文件:
# ubsan.toolchain.cmake
set (CMAKE_C_COMPILER gcc)
set (CMAKE_CXX_COMPILER g++)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS_INIT "-O0 -g -fsanitize=undefined -fno-omit-frame-pointer -fno-var-tracking-assignments")
set (CMAKE_BUILD_TYPE Debug)
我的问题是这是否是通过cmake 链接asan 和pthread 的正确方法,因为我对使用消毒剂的世界还很陌生。
感谢您的任何见解!
编辑:修正错字和陈述的问题。
【问题讨论】:
-
本着本论坛的精神,您的问题是什么?
-
感谢 cmets。我已经修复了错字,不幸的是它并没有改变与链接相关的任何内容,但无论如何正确都绝对重要!