Linking problems with cmake

I have a simple project written in CUDA to do simple calculations on a GPU. My project is structured like so:

…root/
…build/
…src/
…CMakeLists.txt
…main.cu
…cu/
…CMakeLists.txt
.cu
.cpp
…cuh/
.cuh
.h

When I compiled on the command line, I needed to include -dc flag to nvcc or else I get the following error:
ptxas fatal : Unresolved extern function ‘_ZN9potDriver15createPotDriverEi’

Now I’m trying to use cmake to do the same but I keep getting the same error above. Here is my root/src/CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 3.12 FATAL_ERROR)

#about this project
#------------------
project(mcpc LANGUAGES CXX CUDA)
set(VERSION_MAJOR "0")
set(VERSION_MINOR "05")
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}")

#setting the output directory
#------------------
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

#enabling C++11 flag
#------------------
set(
 CUDA_NVCC_FLAGS
 ${CUDA_NVCC_FLAGS};
 -std=c++11

 )

#I think I'm enabling seperable compilation
set(CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE ON)
set(BUILD_SHARED_LIBS OFF)
set(CUDA_SEPARABLE_COMPILATION ON)

#set env
#------------------
include_directories(${PROJECT_SOURCE_DIR}/cuh)

#add subfolders
#------------------
add_subdirectory(cu)

add_executable(main.o ${PROJECT_SOURCE_DIR}/main.cu)
target_link_libraries(main.o cu cuCore)

and root/src/cu/CMakeLists.txt

add_library(cuCore STATIC
 test1.cpp
 test2.cu
 test3.cu
 )

To compile I cd into my build directory and run
cmake …/src/

-bash-4.2$ cmake ../src
-- The CXX compiler identification is GNU 7.3.0
-- The CUDA compiler identification is NVIDIA 9.2.88
-- Check for working CXX compiler: /apps/gcc/7.3.0/bin/c++
-- Check for working CXX compiler: /apps/gcc/7.3.0/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working CUDA compiler: /apps/cuda/9.2.88/bin/nvcc
-- Check for working CUDA compiler: /apps/cuda/9.2.88/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /fslhome/pkawak/mcpc/build

make

ptxas fatal   : Unresolved extern function '_ZN9test315test3doEi'
make[2]: *** [cu/CMakeFiles/cuCore.dir/test2.cu.o] Error 255
make[1]: *** [cu/CMakeFiles/cuCore.dir/all] Error 2
make: *** [all] Error 2

Can anyone help? What am I doing wrong? Much appreciated!

Use one CMakeLists.txt file for the project, not two.
Do it in a way as in the following Working example:


Works via the macros provided in ‘FindCuda’ cmake file - especially the ‘cuda_compile’ macro.

Newer CMAKE versions have also ‘native’ CUDA support, see https://devblogs.nvidia.com/building-cuda-applications-cmake/

Thank you for your prompt answer HannesF99. I will attempt this.

Worked like a charm! I actually looked at that before but missed a few essential points due to novicity.