C++ 在适用于 Xcode 的 CMake 中启用 AddressSanitizer 的正确方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44320465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the proper way to enable AddressSanitizer in CMake that works in Xcode
提问by ObjSal
I've added AddressSanitizer flag as follow:
我添加了 AddressSanitizer 标志如下:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
Everything builds and runs fine when using Unix Makefiles
.
使用Unix Makefiles
.
The problem comes when generating the Xcode project, it just doesn't want to link because it cannot find the ASan library.
问题出现在生成Xcode项目时,它只是不想链接,因为它找不到ASan库。
I already found two solutions, but decided not to use them because they cannot be automated using just CMake:
我已经找到了两个解决方案,但决定不使用它们,因为它们无法仅使用 CMake 实现自动化:
- Adding
-Wl,-undefined,dynamic_lookup
to the linked flags, so it skips linking to dynamic libraries. - Link with
libclang_rt.asan_osx_dynamic.dylib
directly.
- 添加
-Wl,-undefined,dynamic_lookup
到链接标志,因此它跳过链接到动态库。 libclang_rt.asan_osx_dynamic.dylib
直接链接。
So what's the problem with these two solutions?
那么这两种解决方案有什么问题呢?
- When using solution #1, I have to manually open the target scheme in Xcode and add
DYLD_INSERT_LIBRARIES
environment variable pointing tolibclang_rt.asan_osx_dynamic.dylib
. - When using solution #2, the path for the ASan library varies between computers.
- 使用解决方案 #1 时,我必须在 Xcode 中手动打开目标方案并添加
DYLD_INSERT_LIBRARIES
指向libclang_rt.asan_osx_dynamic.dylib
. - 使用解决方案#2 时,ASan 库的路径因计算机而异。
Additionally as another solution, I tried enabling Address Sanitizer flag from the Xcode target scheme but interestingly it didn't detect the issues I added, so I didn't list this as a solution because it failed my test.
此外,作为另一种解决方案,我尝试从 Xcode 目标方案启用 Address Sanitizer 标志,但有趣的是它没有检测到我添加的问题,所以我没有将其列为解决方案,因为它没有通过我的测试。
Any help will be much appreciated.
任何帮助都感激不尽。
回答by MaartenVds
You need to provide the flag(s) to the linker too. I'm doing it like this:
您还需要向链接器提供标志。我是这样做的:
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
回答by zxshi
cmake 3.13
introduce configuration for xcode schema
cmake 3.13
引入xcode 模式的配置
in CMake
在 CMake 中
cmake_minimum_required(VERSION 3.13)
set(CMAKE_XCODE_GENERATE_SCHEME ON)
set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER ON)
set(CMAKE_XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN ON)
When Build
构建时
xcodebuild -enableAddressSanitizer YES
回答by Unapiedra
The simplest solution that I currently found is
我目前找到的最简单的解决方案是
cmake -DCMAKE_BUILD_TYPE=ASAN .
I prefer this option since it expresses the intent (run sanitizers) rather than modifies a number of flags.
我更喜欢这个选项,因为它表达了意图(运行消毒剂)而不是修改许多标志。
I do not know when this option was added. I also cannot find any documentation to it.
我不知道这个选项是什么时候添加的。我也找不到任何文档。