我正在开发我的第一个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编译器似乎非常挑剔。
答案 0 :(得分:3)
您需要向nvcc添加-std=c++11
选项以进行编译。通过使用std名称空间,您将与std::distance
发生冲突,该冲突要求c ++ 11或更高版本才能使用nvcc进行编译。
这有效:
$ cat bugaboo.cu
#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 --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Tue_Jun_12_23:07:04_CDT_2018
Cuda compilation tools, release 9.2, V9.2.148
$ nvcc --std=c++11 -o bugaboo bugaboo.cu
$ ./bugaboo
distance = 6
这不是:
$ nvcc -o bugaboo bugaboo.cu
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(165): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(166): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(167): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(168): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: a class or namespace qualified name is required
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: global-scope qualifier (leading "::") is not allowed
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
/usr/include/c++/4.8/bits/stl_iterator_base_types.h(169): error: expected a ";"
detected during instantiation of class "std::iterator_traits<_Iterator> [with _Iterator=double]"
bugaboo.cu(10): here
15 errors detected in the compilation of "/tmp/tmpxft_00000acd_00000000-8_bugaboo.cpp1.ii".