Ошибка CUDA: имя, сопровождаемое "::", должно быть классом или пространством имен - PullRequest
2 голосов
/ 03 октября 2019

Я работаю над своей первой программой 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 Ответ

3 голосов
/ 04 октября 2019

Вам нужно добавить опцию -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".
...