[英]LLVM address sanitizer with CMake
I am trying to compile the simplest executable using clang with -fsanitize=address option.我正在尝试使用带有 -fsanitize=address 选项的 clang 编译最简单的可执行文件。 It's very simple to do that using clang directly.
直接使用 clang 来做到这一点非常简单。 But my point is to do it through CMake.
但我的观点是通过 CMake 做到这一点。
There is how I am doing it.我就是这样做的。 CMakeLists.txt file:
CMakeLists.txt 文件:
cmake_minimum_required(VERSION 3.5.1 FATAL_ERROR)
project(TestSanitizer VERSION 0.1.0 LANGUAGES CXX)
add_executable(Test main.cpp)
target_compile_options(Test PUBLIC
-std=c++17
-Werror
-Weverything
-Wno-c++98-compat
-Wno-c++98-c++11-c++14-compat
-Wno-c++98-compat-pedantic
-fsanitize=address)
main.cpp:主.cpp:
int main(int, const char**) { return 0; }
configure and make using bash script (config_gen_build.sh):使用 bash 脚本(config_gen_build.sh)配置和制作:
if [ -d "bin" ]; then
rm -rf bin
fi
mkdir bin
cd bin
#config and gen
export CC=/usr/bin/clang-5.0
export CXX=/usr/bin/clang++-5.0
cmake ./../
#build
make
Finally, the error I am getting:最后,我得到的错误:
What's wrong here?这里有什么问题? Should I link with some library?
我应该链接一些图书馆吗?
The easiest way to link with the address sanitizer is to specify -fsanitize=address
as a linker, as well as a compiler, option.与地址清理器链接的最简单方法是将
-fsanitize=address
指定为链接器和编译器选项。 This causes clang or gcc to pass the correct libraries and flags to the linker.这会导致 clang 或 gcc 将正确的库和标志传递给链接器。
I believe you forgot to pass -fsanitize
to the link options as the other answer mentioned.我相信您忘记将
-fsanitize
传递给链接选项,就像提到的其他答案一样。 So you need both :所以你需要两者:
target_compile_options(Foo PUBLIC -fsanitize=address)
target_link_options(Foo PUBLIC -fsanitize=address)
Alternatively, to enable this for you entire project, you can use the following CMake directives at the beginning of your project file:或者,要为整个项目启用此功能,您可以在项目文件的开头使用以下 CMake 指令:
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.