CUDA: nvlink error : Undefined reference to when nvcc compiler forced to compile .cpp file as .cu file using cmake 3.11.1
- [cmake version] 3.11.1 version
- [platform] Linux Mint 18
- [experiment] I want to make static library of .cpp code that can be called from both host and device code. Finally, I want to link it with .cu object file. To achieve my objective, I changed the cpp compiler to nvcc and set the compiler flag to -x=cu. This helped to build the library. Then, I tried to link .cu object file with the library. During building library and .cu object file, separate compilation was on.
- [result] I get an error: undefined reference Undefined reference to '_ZN8particle7advanceEf'.
- [code] I am using the code from the Nvidia blogpost https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/. And, created my Cmake file which is attached with this message.
- CMakeLists.txt
- [questions] (a) In latest version of cmake, is it even possible to force nvcc compiler to treat .cpp file as .cu files? If it is not an issue with cmake then I would still be interesting in understanding how cmake is behaving in this case. If you need more information from me then please let me know.
Edit 1 - Here is the result from make verbose
- It works when all the files in .cu format. In this case, nvcc first links the executable with the static library then g++ links it with cuda specific libraries.
- In case, particle.cpp and v3.cpp are forced to compile by nvcc. First it tries to link with cuda specific libraries where it can not find the reference to '_ZN8particle7advanceEf' and gives an error.
Edited by Bhupesh19