nvcc compilation: Invalid qualifiers on non-member function

I am trying to install from source a project on another system that I have successfully compiled in one so far. This project has been tested in many other systems so it is most likely not a bug (or not a major bug) in the code. The main code base is a mixture of .cpp, .hpp and .cu files.

At the beginning, the Makefile calls the g++ compiler to produce the objects of some .cpp files. After that, the Makefile calls the nvcc compile the .cu files and produce the equivalent .cuo object files. There is where the problems start. The first file that is being compiled with the nvcc (the first .cu file) #includes a .hpp file that has the following code lines (and is causing the compilation errors):

// other lines of code ...
#include <math.h>

#define DEFINE_VSL_UNARY_FUNC(name, operation) \
  template<typename Dtype> \
  void v##name(const int n, const Dtype* a, Dtype* y) { \
    CHECK_GT(n, 0); CHECK(a); CHECK(y); \
    for (int i = 0; i < n; ++i) { operation; } \
  } \
  inline void vs##name( \
    const int n, const float* a, float* y) { \
    v##name<float>(n, a, y); \
  } \
  inline void vd##name( \
      const int n, const double* a, double* y) { \
    v##name<double>(n, a, y); \
  }

DEFINE_VSL_UNARY_FUNC(Sqr, y[i] = a[i] * a[i]);
DEFINE_VSL_UNARY_FUNC(Exp, y[i] = exp(a[i]));
DEFINE_VSL_UNARY_FUNC(Abs, y[i] = fabs(a[i]));

// other lines of code ...

When this code is #included to .cpp files that are compiled with the g++ it returns no errors. On the other hand, when it is trying to compile the .cu file with the nvcc I am getting the following errors (and many others related to functions defined on the same way with the function factory pattern):

./included_file.hpp: In function ‘void vSqr(int, const Dtype*, Dtype*)’:
./include_file.hpp:34:378: error: invalid qualifiers on non-member function type
 DEFINE_VSL_UNARY_FUNC(Sqr, y[i] = a[i] * a[i]);
                                                                                                 
./included_file.hpp:34:378: error: invalid qualifiers on non-member function type
./included_file.hpp:34:561: error: invalid qualifiers on non-member function type
 DEFINE_VSL_UNARY_FUNC(Sqr, y[i] = a[i] * a[i]);

./included_file.hpp:34:561: error: invalid qualifiers on non-member function type
./included_file.hpp: In function ‘void vExp(int, const Dtype*, Dtype*)’:
./included_file.hpp:35:378: error: invalid qualifiers on non-member function type
 DEFINE_VSL_UNARY_FUNC(Exp, y[i] = exp(a[i]));
                                                                                                               
./included_file.hpp:35:378: error: invalid qualifiers on non-member function type
./included_file.hpp:35:561: error: invalid qualifiers on non-member function type
 DEFINE_VSL_UNARY_FUNC(Exp, y[i] = exp(a[i]));

// Many more errors of the same type (related to functions defined with the function factory pattern)

My system is:

OS: Ubuntu 14.04
g++ -v: gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~12.04) - there is also g++ v4.4 installed but the v4.8 is in use.

GPU name: Tesla K20c
Nvidia driver: 340.29 (from the nvidia-smi command)
CUDA version: 6.5 (also tested with 6.0, but returns the same erros)
nvcc -V: release 6.5, V6.5.12

I have searched for the past few days and did not find anything relevant to this anywhere. I do not think that there is anything wrong with the code, because it is tested on other systems and no one has reported something like that. There is also an CPU-only version (without using the nvcc and compiling the CUDA code) which works fine. I would much appreciate any help on this; if you know what it might be wrong.

Thank you in advance!

For these kind of problems, posting a complete, self-contained, buildable code is highly recommended. After supplying missing bits, such as definitions of CHECK(), and correcting the obvious mistake in Sqr the code compiles without any issues for me, using CUDA 6.5.

Thank you for your comment. In fact, the CHECK() function is the one who crates the problems. This function (along with others) is defined in the google-glog library (https://code.google.com/p/google-glog/). I have successfully installed the libgoogle-glog-dev package and I can confirm that the header files are in /usr/include/glog/ directory.

Even though this glog/logging.h header file is #included directly into included_file.hpp, the nvcc compiler sends these errors when compiling a .cu file that includes the included_file.hpp. When the included_file.hpp is included into a .cpp file, the g++ does not return an error.

Also the /usr/include/ path is included by default to the g++, so it is not included with the -I option during compilation. I have trying to included it, but the outcome is the same.

What I have found out about the gcc compiler installed is that the pre-processor has a different version:

  • cpp --version returns: cpp (Ubuntu/Linaro 4.6.4-1ubuntu1~12.04) 4.6.4
  • gcc --version returns: gcc (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
  • g++ --version returns: g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1

Also notice that Ubuntu 14.04 is installed in that system and the gcc might have been installed before the system upgraded from the 12.04. I do not know if that might cause some problem, but I am mentioning it before it seems weird to me.

I have made a more concrete example, and I am posting the information here:

  1. I have installed the glog library and also have the gcc version as mentioned above.

  2. I have a file called common.hpp:

#include <glog/logging.h>
  1. Another file called parent.hpp:
#include "common.hpp"
#include <iostream>

class TestClass {
  
  virtual void TestF1() {
    int var =0;
    CHECK(var);
  }

  virtual int TestF2() {
    int k = 0;
    CHECK(k);
    std::cout << "CHECK - Pass" << std::endl;
  }
};
  1. And my last file is the child.cu:
#include "parent.hpp"

When I am compiling with:

/usr/local/cuda-6.5/bin/nvcc --ccbin=g++ -Xcompiler -fPIC -O2 -I/usr/include/ child.cu

I am getting the following errors:

parent.hpp: In member function ‘virtual void TestClass::TestF1()’:
parent.hpp:8:75: error: invalid qualifiers on non-member function type
     CHECK(var);

parent.hpp:8:75: error: invalid qualifiers on non-member function type
parent.hpp: In member function ‘virtual int TestClass::TestF2()’:
parent.hpp:13:73: error: invalid qualifiers on non-member function type
     CHECK(k);

parent.hpp:13:73: error: invalid qualifiers on non-member function type

(The same error happen even if I am compiling with nvcc child.cu)

These errors do not show up when I am compiling with g++ the file child.cpp:

#include "parent.hpp"

All files are in the same directory and logging.h is at /usr/include/glog/

A quick Google search for the text of the error message suggests that what you are observing is due to a known bug in g++ 4.8.1 that was fixed in g++ 4.8.2.

Please note that in general, the host code passed by nvcc to the host compiler is not necessarily a verbatim copy of the original source code, so the code sent to host compiler by nvcc could trigger the bug while the original code compiled directly by the host compiler does not.

If it is straightforward to switch to g++ 4.8.2, I would suggest giving that a try. You may have to point nvcc at the host compiler of your choice using command line flags, I have no experience with that.

Unfortunately, that was the error. Thank you for pointing it out.