在CMake中启用在Xcode中工作的AddressSanitizer的正确方法是什么 [英] What's the proper way to enable AddressSanitizer in CMake that works in Xcode
问题描述
我添加了AddressSanitizer标志,如下所示:
I've added AddressSanitizer flag as follow:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
使用 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_lookup
to the linked flags, so it skips linking to dynamic libraries. - Link with
libclang_rt.asan_osx_dynamic.dylib
directly.
这两个解决方案有什么问题?
So what's the problem with these two solutions?
- 使用解决方案1时,我必须手动打开目标Xcode中的方案,并添加指向
libclang_rt.asan_osx_dynamic.dylib
的DYLD_INSERT_LIBRARIES
环境变量。 - 使用解决方案#2时,ASan库的路径在计算机之间是不同的。
- 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.
另外,作为另一种解决方案,我尝试启用从Xcode目标方案解决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")
这篇关于在CMake中启用在Xcode中工作的AddressSanitizer的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!