[CMake] Using CMake to link C++ and CUDA Object file

Quang Ha quang.t.ha.20 at gmail.com
Fri Jul 6 00:01:30 EDT 2018


Hi all,

Following upon how to perform code linking between CUDA and C++ (here at
https://devblogs.nvidia.com/separate-compilation-linking-cuda-device-code/),
I have successfully come up with a Makefile that is currently working for
my test case:

##################################Makefile###############
##################################
main_objects = main.o
host_objects = host.o
device_objects = device.o

all: $(main_objects) $(host_objects) $(device_objects)
        g++ -O3 -fopenmp $(main_objects) $(host_objects) $(device_objects)
-L/usr/local/cuda/lib64 -lcudart -o main

$(main_objects): $(main_objects:.o=.cpp)
        g++ -O3 -I/usr/local/cuda/include -c $< -o $@

$(host_objects): $(host_objects:.o=.cpp)
        g++ -O3 -DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_OMP -fopenmp -c $<
-o $@

$(device_objects): $(device_objects:.o=.cu)
        nvcc -O3 -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA -c $< -o
$@

clean:
        rm -f *.o main
####################################################################
##################################

Now, I am trying to write a CMake for this, my current attempt is as
followed:

##############################################CMakeLists.txt############################################
cmake_minimum_required(VERSION 3.8)
project(MyTest LANGUAGES CXX CUDA)

# check requirements
find_package(CUDA REQUIRED)
find_package(OpenMP)

if (CUDA_FOUND AND OPENMP_FOUND)
    # OpenMP
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
${OpenMP_EXE_LINKER_FLAGS}")

    # CUDA
    include_directories(${CUDA_INCLUDE_DIRS})
    set(ALL_CUDA_LIBS ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY}
${CUDA_cublas_LIBRARY})
    set(LIBS ${LIBS} ${ALL_CUDA_LIBS})
    set(CUDA_SEPARABLE_COMPILATION OFF)
    set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};--maxrregcount 32)

    set(OMP_SOURCES host.cpp)
    add_library(Host_OMP SHARED ${OMP_SOURCES})
    set_target_properties(Host_OMP PROPERTIES COMPILE_FLAGS
"-DTHRUST_HOST_SYSTEM=THRUST_HOST_SYSTEM_OMP")
    set(LIBS ${LIBS} ${Host_OMP})

    set(CUDA_SOURCES device.cu)
    cuda_add_library(Device_CUDA SHARED ${CUDA_SOURCES})
    set_target_properties(Device_CUDA PROPERTIES COMPILE_FLAGS
"-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CUDA")
    set_target_properties(Device_CUDA PROPERTIES CUDA_SEPERABLE_COMPILATION
ON)
    set(LIBS ${LIBS} ${Device_CUDA})

    add_executable(main main.cpp)
    target_link_libraries(main ${LIBS})

endif(CUDA_FOUND AND OPENMP_FOUND)
####################################################################
##################################

It is still, unfortunately, not working. If I commented out the
add_executable line, the object code for Host_OMP and Device_CUDA is
compiled. I can then use the terminal and link them with main.cpp using gcc
and generate the executable - i.e. using the final command in the Makefile
above.

What am I doing wrong here? It seems to be very fundamental, but I can't
seem to get it right...

Any suggestions would be greatly appreciated!!

Thanks,
Quang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180705/13087b00/attachment.html>


More information about the CMake mailing list