我正在开发我的第一个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
看了这篇帖子后,我开始考虑可能是从主机 / 设备分界线的错误一侧调用了某些内容,但是目前我还没有进行任何GPU调用。
不确定发生了什么,但到目前为止,CUDA编译器似乎非常挑剔。
std::distance
,而你自己的distance
函数似乎正在发生冲突... - Aconcaguadist(...)
解决了问题。 - HMLDudeusing namespace std;
。如果你认为写std::cout
太麻烦,那么可以使用using std::cout;
。此外,你还可以将自己的函数放在自己的命名空间中。 - Aconcaguausing
声明。 - Aconcagua