具有CMakeLists.txt的C ++ AddressSanitizer导致asan错误 [英] C++ AddressSanitizer with CMakeLists.txt results in asan errors
问题描述
尝试在C ++项目上使用 AddressSanitizer 工具(ASan),非常详细的输出,其中包含对<__ asan_report_store8'的未定义引用和对'__asan_report_load8'
的未定义引用,以及其他类似 __ asan_stack_malloc_2
的引用.我的项目是使用CMake构建的,并且已将以下行添加到CMakeLists.txt中:
Trying to use the AddressSanitizer tool (ASan) on my C++ project, I get a very verbose output full of undefined reference to '__asan_report_store8'
and undefined reference to '__asan_report_load8'
, and others like __asan_stack_malloc_2
. My project is built with CMake, and I've added the following line to my CMakeLists.txt:
add_definitions(-fsanitize=address -g3 -fno-omit-frame-pointer)
遵循以下ASan示例:
following these ASan examples:
运行上面的makefile示例,我得到了预期的漂亮输出(也可以在视频链接中看到).但是对于我的项目,我得到了那些错误的混乱堆栈跟踪.
Running the above makefile example I get the expected pretty output (which can also be seen in the video link). But for my project I'm getting a messy stack trace of those errors.
注意:
- ubuntu 16.04
- 我已设置环境变量
ASAN_OPTIONS = symbolize = 1
和ASAN_SYMBOLIZER_PATH =/usr/lib/llvm-5.0/bin/llvm-symbolizer
以便打印ASan输出中的行号
- ubuntu 16.04
- I've set the environment variables
ASAN_OPTIONS=symbolize=1
andASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer
in order to print the line numbers in the ASan output
推荐答案
当编译要使用其中一种LLVM Sanitizer运行的代码时,必须将 -fsanitize = ...
标志传递给编译器和链接器.使用CMake,您可以通过调用 target_link_libraries
:
When compiling code to be run with one of the LLVM Sanitizers, you have to pass the -fsanitize=...
flag to both the compiler and the linker. With CMake, you can do this by calling target_link_libraries
:
target_link_libraries(MyTarget
-fsanitize=address
)
或者,如果您不使用现代CMake,则可以使用 link_libraries
命令
Alternatively, if you aren't using modern CMake, you can do the same with the link_libraries
command
这篇关于具有CMakeLists.txt的C ++ AddressSanitizer导致asan错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!