Search code examples
c++compiler-errorscudanvcc

CUDA Error: name followed by "::" must be a class or namespace


I am working on my first CUDA program and running into error using the nvcc compiler that I do not encounter if I compile with g++.

My code:

#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;
}

If I compile with nvcc test.cu -o test, the result is:

/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

When I change the file extension to .cpp and compile as follows, g++ test.cpp -o test, the code complies. If I then execute ./test, I get the result I am looking for:

distance = 6

Looking at this post inspired me to consider the possibility that I am invoking something from the wrong side of the host/device divide, however, I am not making any GPU calls yet.

Not sure what is going on, but so far the CUDA compiler seems extremely finicky.


Solution

  • You need to add the -std=c++11 option to nvcc to compile this. By using the std namespace, you are getting a conflict with std::distance which requires c++11 or later to compile with nvcc.

    This works:

    $ 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
    

    and this doesn't:

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