[我正在开发我的第一个CUDA程序,并使用nvcc
编译器遇到了错误,而如果使用g++
进行编译,则不会遇到此错误。
我的代码:
#include <iostream>
#include <cmath>
using namespace std;
double distance(double first, double second);
int main(){
double dis;
dis = distance(7.0, 1.0);
cout << "distance = " << dis << endl;
return 0;
}
double distance(double first, double second){
double diff;
diff = abs(first-second);
return diff;
}
如果我使用nvcc test.cu -o test
进行编译,则结果为:
/usr/include/c++/5/bits/stl_iterator_base_types.h(168): error: name followed by "::" must be a class or namespace name
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
test.cu(11): here
/usr/include/c++/5/bits/stl_iterator_base_types.h(169): error: name followed by "::" must be a class or namespace name
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
test.cu(11): here
/usr/include/c++/5/bits/stl_iterator_base_types.h(170): error: name followed by "::" must be a class or namespace name
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
test.cu(11): here
/usr/include/c++/5/bits/stl_iterator_base_types.h(171): error: name followed by "::" must be a class or namespace name
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
test.cu(11): here
/usr/include/c++/5/bits/stl_iterator_base_types.h(172): error: name followed by "::" must be a class or namespace name
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
test.cu(11): here
当我将文件扩展名更改为.cpp并按以下方式编译时,g++ test.cpp -o test
,代码将遵循。如果然后执行./test
,则会得到所需的结果:
distance = 6
[看this帖子激发了我考虑我是否从主机/设备划分的错误方面调用某些内容的可能性,但是,我尚未进行任何GPU调用。
不确定发生了什么,但是到目前为止,CUDA编译器似乎非常挑剔。
-std=c++11
选项添加到nvcc进行编译。通过使用std命名空间,您与std::distance
发生冲突,而这需要c ++ 11或更高版本才能使用nvcc进行编译。