Nvcc not linking using CMake weird

I will try to explain the best I can my problem, I’m not to good with CUDA or CMake.

I’m working on a project and have already create a functional program (prog1) that uses a library lest called lib1 that have a the library source(lib1.cu) and the header (lib.cuh), using CMake I been able to compile and make the program run using separated compilation. I create a two classes “class1, class2” class2 depends on class1 both are compile with no problem.

And this is the part when every thin goes down. In order to use the classes I create a library (lib2) to try no to have a mess I add some functions to lib1 in order to use them in lib2, the added functions do not depend on element in other libraries. When I try to compile all the project I’m able to compile everything except the last library (lib2) the message say a similar message like “nvlink error : Undefined reference to ‘_Z12Neigh_surfeliiiPi’” for all the functions that are in lib1 and used in lib2.

The weird part is if “#ifdef”,"#define", “#endif” are use inside the libraries headers (lib1.cuh, lib2.cuh) as it should be, the lib2 and prog1 does not recognize any function, if are not used I’m able to compile both but unable to link the lib2 with lib1. And if are not use in the classes the lib2 doesn’t recognize the classes.

Any ideas on how to fix the linking for the functions.

You’re rebuilding lib1, right?

Yes, every time I make a change at the CMakelist I erase all building directory and run CMake again

Okay, cool. That probably means you’re not setting up your link targets correctly. Hopefully you’re doing something like:

target_link_libraries(lib2 lib1);

for your linking.

This is assuming you have both build targets in the same CMakeLists.txt file or that you’ve imported lib1 into lib2’s CMake file.

All the corresponding target_link_libraries are properly set I use the following target_link_libraries for the corresponding CMakeLists.txt

  1. lib1 -> TARGET_LINK_LIBRARIES(lib1)
  2. class1-> TARGET_LINK_LIBRARIES(class1)
  3. class2-> TARGET_LINK_LIBRARIES(class2 lib1 class1)
  4. lib2-> TARGET_LINK_LIBRARIES(lib2 lib1 class1)

Or I’m using wrong CMAKE ?