nvlink error : Undefined reference to '_ZNK4_4626Camera16get_aspect_ratioEv'

I’m currently implementing a parallel raytracer. I’ve implemented the serial code and it works fine. To make my life easier I’m using the following macro on all my classes (Vector class, Camera class, and so on):

#ifdef __CUDACC__
#define CUDA_HOSTDEV __host__ __device__

For certain classes(in this case Vector class and Matrix class), i can create objects of that class in my device code. However for other classes(camera , ray and so on), I get the following error:

nvlink error  : Undefined reference to '_ZNK4_4626Camera16get_aspect_ratioEv'

I’m not sure why this is happening and I don’t know even know where to start debugging. I first looked at my CMakeLists.txt which seems to be correct since the serial version works.

If the class method (Camera::get_aspect_ratio() const in this case) is defined in another file, then you will get this error if you are not using relocatable device code (RDC) compile options for nvcc. Or if the class method is not defined anywhere, then you would get an error like this.

If you want help from others, one possible approach is to create a simple reproducer, that is one or two files, built into a project, that will give the error, that someone else could copy, paste and compile. It’s likely that if you create that, someone else could help you. You need to be sure that what you post is complete, and doesn’t require anything to be added or guessed at.

If you have no clue how to do that, then the Stackoverflow MCVE definition:


or this definition:


will be useful, perhaps. The basic idea is to trim what you have down, into something that is complete, but small, that is easy for others to work with and observe the issue.

If you understand what CMake is doing, then there is generally no issue using it. But if you don’t understand how CMake builds things, then when you run into trouble, it’s even harder for you to get help, since you don’t know how to explain how things are getting built. Off the top of my head, I don’t know how to enable RDC in CMake, but google could probably help you out there.

I’m having trouble reproducing the error.