You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下用Clang -fsanitize=array-bounds编译C99代码遇链接错误求助

问题

在Windows系统中使用Clang的-fsanitize=array-bounds编译选项测试C99代码时,出现链接错误。

测试代码:

int index = 11;

int arr[10] = { 0 };
arr[index] = 33;

编译错误信息:

----- Build Debug x64 -----
Executing build command:
clang_build.bat
lld-link: error: undefined symbol: __ubsan_handle_out_of_bounds
>>> referenced by D:\\User Program Files_Data\\software_projects\\bgz_engine\\source\\win64_main.c:223
>>>               win64_main.o:(WinMainCRTStartup)
===== Success : 1 errors, 0 warnings =====

当前使用的clang_build.bat内容:

@echo off

REM This represents the full directory path to your batch file
set cwd=%~dp0

call "C:\\msvc\\setup.bat"

REM -fno-stack-protector tells compiler to not worry about security calls (like __security_cookie) that would typically be inserted with c libs. -mstack-probe-size=99999999999 basically says don't worry about __chkdsk for checking stack overruns
set compiler_flags=-g -fdebug-macro -O0 -fno-stack-protector -Wall -Wpadded -mstack-probe-size=9999999 -fno-builtin -std=c99 -fdiagnostics-absolute-paths -fsanitize=array-bounds

REM -STACK:0x100000,0x100000 is setting function stack default to 1MB. Can increase this if we need to. Keep in mind though this also increases default stack size for all threads created
set linker_flags=-subsystem:windows -machine:x64 -incremental:no -opt:ref -debug:full -ignore:4099 -NODEFAULTLIB -STACK:0x100000,0x100000

set include_paths=-I"%cwd%source" -I"C:\\msvc\\Windows Kits\\10\\Include\\10.0.22621.0\\um"
set library_paths=-LIBPATH:"C:\\msvc\\Windows Kits\\10\\Lib\\10.0.22621.0\\um\\x64" -LIBPATH:"C:\\Program Files\\LLVM\\lib\\clang\\14.0.6\\lib\\windows"
set import_libraries="kernel32.lib" "user32.lib" "glew32s.lib" "opengl32.lib" "gdi32.lib" "clang_rt.ubsan_standalone-x86_64.lib"

IF NOT EXIST bin mkdir bin
pushd bin

clang --compile ..\\source\\editor.c %compiler_flags%
lld-link editor.o -dll %linker_flags% -export:editor_update -noentry

clang --compile ..\\source\\win64_main.c %compiler_flags% %include_paths% -I"%cwd%dep\\glew-2.1.0\\include" -DBGZ_WIN64 -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-null-dereference
lld-link win64_main.o -OUT:win64_main.exe %linker_flags% %library_paths% %import_libraries% -LIBPATH:"%cwd%dep\\glew-2.1.0\\lib\\win64-release"

popd

已确认LLVM 14.0.6安装目录下存在clang_rt.ubsan_standalone-x86_64.lib,推测Sanitizer支持Windows系统。需要解决链接时无法解析__ubsan_handle_out_of_bounds符号的问题。

解决方案

问题出在UBSan独立库的链接顺序以及是否遗漏了依赖库,按以下步骤修改即可:

  • 调整链接库顺序:
    将clang_rt.ubsan_standalone-x86_64.lib移到import_libraries的最后位置。因为链接器是按从左到右的顺序查找符号,把Sanitizer库放在最后能确保它能找到前面目标文件中引用的符号。

    修改后的import_libraries:

    set import_libraries="kernel32.lib" "user32.lib" "glew32s.lib" "opengl32.lib" "gdi32.lib" "clang_rt.ubsan_standalone-x86_64.lib"
    

    (注:如果你的原顺序已经是最后,那执行下一步)

  • 添加UBSan的依赖库:
    UBSan独立库需要依赖clang_rt.builtins-x86_64.lib,这个库同样位于LLVM的lib\clang\14.0.6\lib\windows目录下。将它添加到import_libraries中,放在clang_rt.ubsan_standalone-x86_64.lib之前:

    修改后的import_libraries:

    set import_libraries="kernel32.lib" "user32.lib" "glew32s.lib" "opengl32.lib" "gdi32.lib" "clang_rt.builtins-x86_64.lib" "clang_rt.ubsan_standalone-x86_64.lib"
    
  • 确保编译时的Sanitizer选项一致:
    检查编译editor.c和win64_main.c时都添加了-fsanitize=array-bounds选项,你的脚本中已经做到了这一点,无需修改。

  • 验证库路径正确性:
    确认library_paths中已经包含了LLVM的lib目录:

    -LIBPATH:"C:\\Program Files\\LLVM\\lib\\clang\\14.0.6\\lib\\windows"
    

    你的脚本中已经配置,确保路径无误即可。

完成以上修改后重新运行clang_build.bat,链接错误即可解决。


内容的提问来源于stack exchange,提问作者Jason

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:46:06