CUDA Error: name followed by "::" must be a class or namespace
CUDA Error: name followed by "::" must be a class or namespace
我正在开发我的第一个 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 post 启发我考虑我从 host/device 鸿沟的错误一侧调用某些东西的可能性,但是,我没有进行任何 GPU 调用然而。
不确定发生了什么,但到目前为止,CUDA 编译器似乎非常挑剔。
你需要在 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".
我正在开发我的第一个 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 post 启发我考虑我从 host/device 鸿沟的错误一侧调用某些东西的可能性,但是,我没有进行任何 GPU 调用然而。
不确定发生了什么,但到目前为止,CUDA 编译器似乎非常挑剔。
你需要在 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".