我正在开发一个 C 库,并且有一个测试工具程序,该程序链接到该库以测试功能。在构建测试时,我希望测试工具和库都具有调试符号,因为我使用 Address Sanitizer 和 Leak Sanitizer 来检测内存问题。
库编译命令
/opt/homebrew/opt/llvm/bin/clang -dynamiclib -framework ApplicationServices -lcups lib/macos/*.c -o ./test/macos/libTest.dylib -Wall -Werror -Wextra -fvisibility=hidden -current_version 1.0 -compatibility_version 1.0 -arch arm64 -arch x86_64 -fno-limit-debug-info -g
-fno-limit-debug-info -g
来包含调试符号利用编译命令
/opt/homebrew/opt/llvm/bin/clang ./test/macos/*.c -o ./test/macos/run -L ./test/macos -lTest -Wall -Werror -Wextra -g -fsanitize=address -fsanitize-address-use-after-return=runtime -fsanitize-address-use-after-scope -fno-optimize-sibling-calls -O1
-g
来包含调试符号测试工具调用
ASAN_OPTIONS=detect_stack_use_after_return=1 ASAN_OPTIONS=detect_leaks=1 ./test/macos/run
但是,当我运行测试工具时,我收到以下运行时错误
dyld[3025]: Library not loaded: /var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out
Referenced from: <C216EBD6-3360-33F2-AE1E-CD6FBEB74C74> /<PROJECT DIRECTORY>/test/macos/run
Reason: tried: '/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file), '/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file), '/private/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/private/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file), '/private/var/folders/hp/b8x1q6697_129jnylkn87kh40000gp/T/capability-e861fd/capability-arm64.out' (no such file)
如果我在编译库时删除
-g
选项,我可以成功运行测试工具。但是,当引用编译库内的代码时,我没有在 Address Sanitizer 报告中获得调试符号。
是否有其他方法可以在 C 库中包含调试符号?任何指示将不胜感激!
Apple M3 Pro / Mac OS Sonoma 版本 14.3 (23D56)
clang
Homebrew clang version 17.0.6
Target: arm64-apple-darwin23.3.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
在编译导致该库的库时,我能够识别标志的组合。
如果我使用
-arch arm64 -arch x86_64
编译来构建可以在两种架构上加载的胖二进制文件,那么我会收到此运行时错误。但是,删除这些标志并仅包含 arm64 构建将允许库加载而不会出现问题。