问题描述
我添加了 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 的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)