1

I am trying to configure a project with CMake a simple code composed of:

main.cpp
func.cpp
func.cu
func.hpp

To succesfully compile this I did:

nvcc -dc -x cu -c main.cpp -o main.cpp.o -DCUDA                                                                                                   
nvcc -dc -x cu -c func.cu -o func.cu.o -DCUDA                                                                                                     
nvcc -dc -x cu -c func.cpp -o func.cpp.o -DCUDA                                                                                                   
nvcc main.cpp.o func.cu.o func.cpp.o -o main-cuda -lcudart -lcuda

Now I want to do it automatically with CMake making the CUDA compilation optional. My CMakeLists.txt is:

cmake_minimum_required(VERSION 3.11)                                                                                                                      

project(cmake-test LANGUAGES CXX)                                                                                                                         

option(ENABLE_CUDA "Enable CUDA" OFF)                                                                                                                     
if(ENABLE_CUDA)                                                                                                                                           
  enable_language(CUDA)                                                                                                                                   
  set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -DCUDA -dc")                                                                                                         
endif()                                                                                                                                                                                                                                                                            

if(ENABLE_CUDA)                                                                                                                                           
    file(GLOB SOURCES main.cpp func.cu func.cpp)                                                                                                      
else()                                                                                                                                                    
    file(GLOB SOURCES main.cpp func.cpp)                                                                                                                  
endif()                                                                                                                                                   

add_executable(cmake-test ${SOURCES})

But during compilation and linkage CMake uses g++ to compile func.cpp. Is there a way to force CMake to use nvcc also for func.cpp. Inside func.cpp I have a __device__ function that I need in func.cu that is why I am needing to do this (I want to keep the original structure of the code).

gagiuntoli
  • 475
  • 5
  • 13

0 Answers0