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

NVCC编译报错:cospi等函数异常规范与先前声明不兼容

编译CUDA程序时出现异常规范不兼容错误的原因与解决

编译vectorAdd这类简单CUDA程序时,出现如下编译错误:

$ nvcc -I ../../../Common/ -ccbin g++-13 -Wno-deprecated-gpu-targets vectorAdd.cu 
/usr/include/x86_64-linux-gnu/bits/mathcalls.h(79): error: exception specification is incompatible with that of previous function "cospi" (declared at line 2601 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
   extern double cospi (double __x) noexcept (true); extern double __cospi (double __x) noexcept (true);
                                    ^

/usr/include/x86_64-linux-gnu/bits/mathcalls.h(81): error: exception specification is incompatible with that of previous function "sinpi" (declared at line 2556 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
   extern double sinpi (double __x) noexcept (true); extern double __sinpi (double __x) noexcept (true);
                                    ^

/usr/include/x86_64-linux-gnu/bits/mathcalls.h(79): error: exception specification is incompatible with that of previous function "cospif" (declared at line 2623 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
   extern float cospif (float __x) noexcept (true); extern float __cospif (float __x) noexcept (true);
                                   ^

/usr/include/x86_64-linux-gnu/bits/mathcalls.h(81): error: exception specification is incompatible with that of previous function "sinpif" (declared at line 2579 of /usr/local/cuda/bin/../targets/x86_64-linux/include/crt/math_functions.h)
   extern float sinpif (float __x) noexcept (true); extern float __sinpif (float __x) noexcept (true);
                                   ^

4 errors detected in the compilation of "vectorAdd.cu".

系统信息

  • Devuan GNU/Linux Excalibur(近似无systemd的Debian Trixie)
  • 手动指定g++-13(系统默认g++-14)
  • 内核版本6.12.22-amd64
  • CUDA 12.8.1

原因分析

这个错误源于系统C标准库头文件与CUDA头文件中同名数学函数的异常规范不匹配:

  • 系统的mathcalls.h里,cospi、sinpi及其单精度版本cospif、sinpif被声明为带noexcept(true)异常规范;
  • 而CUDA的math_functions.h中,这些函数的旧声明未添加noexcept属性,导致编译器判定两个声明冲突。

触发问题的常见原因是系统更新了glibc(Debian Trixie的glibc版本较新,新增了这些函数的异常规范),或是切换到了对异常规范检查更严格的GCC版本(如g++-13)。

解决办法

1. 调整GCC版本匹配CUDA要求

查看CUDA 12.8官方支持文档,确认兼容的GCC版本范围。如果g++-13不在兼容列表内,切换到CUDA官方推荐的GCC版本(如g++-12),编译时通过-ccbin指定:

nvcc -I ../../../Common/ -ccbin g++-12 -Wno-deprecated-gpu-targets vectorAdd.cu

2. 强制调整头文件包含顺序

编译时添加-include选项,让CUDA的数学函数头文件先被包含,覆盖系统头文件的声明:

nvcc -I ../../../Common/ -ccbin g++-13 -include /usr/local/cuda/include/crt/math_functions.h -Wno-deprecated-gpu-targets vectorAdd.cu

3. 禁用系统头文件的异常规范

通过定义宏关闭系统头文件中这些函数的noexcept声明:

nvcc -I ../../../Common/ -ccbin g++-13 -D_GLIBCXX_USE_NOEXCEPT=0 -Wno-deprecated-gpu-targets vectorAdd.cu

或使用-D__NO_MATH_INLINES宏,避免系统头文件展开这些函数的内联声明:

nvcc -I ../../../Common/ -ccbin g++-13 -D__NO_MATH_INLINES -Wno-deprecated-gpu-targets vectorAdd.cu

4. 更新CUDA到兼容版本

若上述方法无效,可等待NVIDIA发布兼容新glibc/GCC版本的CUDA补丁,或直接升级到后续CUDA版本(新版本通常会同步系统头文件的函数声明属性)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 06:31:03