gpt4 book ai didi

c++ - CUDA 错误 : name followed by "::" must be a class or namespace

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:20 25 4
gpt4 key购买 nike

我正在处理我的第一个 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 进行编译。

这个有效:

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

关于c++ - CUDA 错误 : name followed by "::" must be a class or namespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58224915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com