3

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.

talonmies
  • 70,661
  • 34
  • 192
  • 269
HMLDude
  • 1,547
  • 7
  • 27
  • 47
  • 1
    Off-topic: About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 03 '19 at 18:52
  • 2
    Hm, if reading the error messages again, my previous comment seems to be less off-topic than it appeared at first - there's already [`std::distance`](https://en.cppreference.com/w/cpp/iterator/distance), and your own `distance` function seems to be colliding with... – Aconcagua Oct 03 '19 at 18:55
  • @Aconcagua, you nailed it!. Changing my function name to ```dist(...)``` solved the problem. – HMLDude Oct 03 '19 at 19:00
  • 2
    Better: Just don't have `using namespace std;` If writing `std::cout` *really* appears too much work for you, then have `using std::cout;` instead. You could additionally place your own functions in your own namespace. – Aconcagua Oct 03 '19 at 19:10
  • @Aconcagua Here's a suggestion: if you are so motivated to help as to leave a comment on someone's question, try doing it in a manner that does not come off as condescending Pro tip: keep in mind that some people are new to software development and/or specific programing languages. – HMLDude Oct 07 '19 at 15:02
  • Well, not my intention to condescend *anything* – sorry if my comment could be perceived as such (not a native English speaker, so you might pardon me). Please understand my comment simply as (strong) recommendation to prefer qualified names over `using`-declarations. – Aconcagua Oct 07 '19 at 16:57

1 Answers1

3

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".
talonmies
  • 70,661
  • 34
  • 192
  • 269