CUDA 11.5中使用Thrust时nvcc编译报错问题咨询
最初编译包含<algorithm>的代码时出现错误,添加-std=c++14编译选项(如nvcc -std=c++14 test1.cu)后问题解决。但引入<thrust/extrema.h>时,再次出现相同报错。
代码示例
#include <algorithm> #include <thrust/extrema.h> // 移除该行可正常编译 int main(){ return 0; }
报错信息
/usr/include/c++/11/bits/std_function.h:435:145: error: parameter packs not expanded with ‘...’:
435 | function(_Functor&& __f)
| ^
/usr/include/c++/11/bits/std_function.h:435:145: note: ‘_ArgTypes’
/usr/include/c++/11/bits/std_function.h:530:146: error: parameter packs not expanded with ‘...’:
530 | operator=(_Functor&& __f)
| ^
/usr/include/c++/11/bits/std_function.h:530:146: note: ‘_ArgTypes’
环境版本
- Ubuntu 22.04.1 LTS
- CUDA编译工具11.5(V11.5.119)
- gcc 11.3.0
- g++ 11.3.0
问题原因
这是CUDA 11.5与GCC 11.x之间的兼容性问题:
- GCC 11.x的C标准库实现里,
std::function的部分模板代码依赖C14及以上的语法特性。CUDA 11.5的Thrust库在引入头文件时,会强制将主机端代码的编译标准回退到C++11,直接覆盖你手动指定的-std=c++14选项。 - 单独包含
<algorithm>时,-std=c++14能让GCC以C14标准编译,避开C11模式下std::function的模板展开错误;但引入<thrust/extrema.h>后,Thrust的内部配置会修改编译标准,导致主机端代码又切换回C++11模式,触发GCC 11.x在该模式下的语法报错。
内容的提问来源于stack exchange,提问作者Kaz

