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

使用Clang的Undefined Behavior Sanitizer时128位整数运算遇链接错误

解决Clang启用UBSan时__int128乘法的__muloti4未定义引用问题

如果你在Clang中使用128位整数(__int128_t),同时开启Undefined Behavior Sanitizer(编译时添加-fsanitize=undefined),大概率会碰到链接错误,提示未定义引用__muloti4。下面是完整的问题复现和解决方法:

问题复现

测试代码example.c:

__int128_t a;
int main (void) {
 a = a * a;
 return 0;
}

编译命令:

clang -fsanitize=undefined example.c

报错信息:

/tmp/example-df4873.o: In function `main':
example.c:(.text+0x4c): undefined reference to `__muloti4'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

(测试环境:Ubuntu 17.10)

问题原因

这是因为UBSan启用时,Clang会为__int128_t的乘法操作生成对__muloti4函数的调用——这个函数负责执行带溢出检查的128位整数乘法。但在Ubuntu 17.10这类环境中,默认链接配置没有包含提供该函数的支持库,因此触发了未定义引用错误。

解决方案

这里有两种可行的解决方式,你可以根据需求选择:

  • 链接UBSan支持库
    先确保Ubuntu系统中安装了对应的依赖包(比如libubsan0),然后修改编译命令,显式链接libubsan库:

    clang -fsanitize=undefined example.c -lubsan
    

    若你的系统中存在compiler-rt库,也可以选择链接它:

    clang -fsanitize=undefined example.c -lcompiler_rt
    
  • 禁用整数溢出检查(按需选择)
    如果你不需要UBSan检查整数溢出类行为,可以添加-fno-sanitize=integer参数关闭这部分检查,这样Clang就不会生成对__muloti4的调用了:

    clang -fsanitize=undefined -fno-sanitize=integer example.c
    

内容的提问来源于stack exchange,提问作者Philipp Claßen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:32:49