CUDA错误:后跟“::”必须是类或命名空间

3

我正在开发我的第一个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编译器似乎非常挑剔。


1
离题:关于 using namespace std…… - Aconcagua
2
嗯,如果再次阅读错误消息,我的先前评论似乎比起初看起来要不那么离题 - 已经有了std::distance,而你自己的distance函数似乎正在发生冲突... - Aconcagua
@Aconcagua,你太棒了!将我的函数名称更改为dist(...)解决了问题。 - HMLDude
2
更好的做法是:不要使用 using namespace std;。如果你认为写 std::cout 太麻烦,那么可以使用 using std::cout;。此外,你还可以将自己的函数放在自己的命名空间中。 - Aconcagua
@Aconcagua 这里有一个建议:如果你如此积极地想要帮助别人并留下评论,尝试以一种不显得轻蔑的方式来做。专业提示:请记住,有些人是新手,对软件开发和/或特定编程语言不熟悉。 - HMLDude
很抱歉如果我的评论可能被认为是居高临下的(我不是母语为英语,希望你能原谅),但我的意图并非如此。请理解我的评论只是强烈建议使用限定名而不是using声明。 - Aconcagua
1个回答

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".

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接