What#39;s the proper way to enable AddressSanitizer in CMake that works in Xcode(在适用于 Xcode 的 CMake 中启用 AddressSanitizer 的正确方法是什么)
问题描述
我添加了 AddressSanitizer 标志如下:
I've added AddressSanitizer flag as follow:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
使用 Unix Makefiles 时,一切构建和运行良好.
Everything builds and runs fine when using Unix Makefiles.
问题是在生成Xcode项目的时候,它只是不想链接,因为它找不到ASan库.
The problem comes when generating the Xcode project, it just doesn't want to link because it cannot find the ASan library.
我已经找到了两个解决方案,但决定不使用它们,因为它们无法仅使用 CMake 实现自动化:
I already found two solutions, but decided not to use them because they cannot be automated using just CMake:
- 将
-Wl,-undefined,dynamic_lookup添加到链接标志,因此它跳过链接到动态库. - 直接与
libclang_rt.asan_osx_dynamic.dylib链接.
- Adding
-Wl,-undefined,dynamic_lookupto the linked flags, so it skips linking to dynamic libraries. - Link with
libclang_rt.asan_osx_dynamic.dylibdirectly.
那么这两种解决方案有什么问题?
So what's the problem with these two solutions?
- 使用解决方案 #1 时,我必须在 Xcode 中手动打开目标方案并添加指向
libclang_rt.asan_osx_dynamic.dylib的DYLD_INSERT_LIBRARIES环境变量. - 使用解决方案 #2 时,ASan 库的路径因计算机而异.
此外,作为另一种解决方案,我尝试从 Xcode 目标方案启用 Address Sanitizer 标志,但有趣的是它没有检测到我添加的问题,所以我没有将其列为解决方案,因为它没有通过我的测试.
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.
任何帮助将不胜感激.
推荐答案
您也需要向链接器提供标志.我是这样做的:
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")
这篇关于在适用于 Xcode 的 CMake 中启用 AddressSanitizer 的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在适用于 Xcode 的 CMake 中启用 AddressSanitizer 的正确方法是什么
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
