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

C++使用__uint128_t报was not declared in this scope错误如何解决

报错原因
  • 你使用的MinGW GCC默认编译目标为32位x86架构,__uint128_t仅支持64位的x86_64/arm64等目标架构,32位模式下GCC不会暴露该内置类型。
  • 编译时指定了纯C++标准参数(如-std=c++17)未启用GNU扩展,__uint128_t属于GNU扩展类型,纯标准模式下不会被定义。
  • 部分旧版MinGW.org的32位发行版本身未适配128位整数支持,即使强制开启相关参数也无法使用该类型。
解决方案

方案1:调整编译参数

  1. 在编译命令中添加-m64参数,指定生成64位程序,即可正常使用__uint128_t类型。注意如果你的编译参数中已经存在-m32,需要先删除该参数。
  2. 如果编译时使用了纯C++标准参数(如-std=c++17),将其替换为对应GNU扩展标准参数(如-std=gnu++17),启用GNU扩展支持。

方案2:手动实现无溢出乘法取模(兼容32位编译环境)

如果必须生成32位程序,可自行实现64位整数乘法取模逻辑替代__uint128_t的使用,示例代码如下:

// 64位整数乘法取模,避免溢出
u64 mul_mod(u64 a, u64 b, u64 mod) {
    u64 res = 0;
    a %= mod;
    while (b) {
        if (b & 1) {
            res = (res + a) % mod;
        }
        a = (a << 1) % mod;
        b >>= 1;
    }
    return res;
}

// 修改后的快速幂实现
u64 binpower(u64 base, u64 e, u64 mod) {
    u64 result = 1;
    base %= mod;
    while (e) {
        if (e & 1)
            result = mul_mod(result, base, mod);
        base = mul_mod(base, base, mod);
        e >>= 1;
    }
    return result;
}

方案3:更换更完善的MinGW发行版

替换掉旧的MinGW.org版本,改用MSYS2提供的MinGW-w64发行版,该版本的x86_64编译器默认完整支持__uint128_t等GNU扩展,兼容性更好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 07:09:04