Dominik
Dominik

Reputation: 21

Cuda CMake 3.10 CMakeLists.txt

I have a visual c++ project which creates a dll. For this project I have a working CMakeLists.txt.

Now I created two cuda source files which complete the project and with visual studio the build works fine. I want to add the matching commands to my cmake file. Can anyone tell me the basic commands I need to add?

I try to build a dll library where i use .cu and .cpp files.... The important part of my cmake file looks like:

# ----------------------------------------------------------------------------
# Set Cuda properties
# ----------------------------------------------------------------------------
enable_language(CUDA)
set(CUDA_SEPARABLE_COMPILATION ON)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
if (CMAKE_SIZEOF_VOID_P MATCHES 8)
  set(CUDA_64_BIT_DEVICE_CODE_DEFAULT ON)
endif()
set(CUDA_NVCC_FLAGS "-gencode arch=compute_50,code=sm_50;-rdc=true;-use_fast_math")

message(STATUS "CUDA_PROPAGATE_HOST_FLAGS: ${CUDA_PROPAGATE_HOST_FLAGS}")
message(STATUS "CUDA_HOST_COMPILER: ${CUDA_HOST_COMPILER}")
message(STATUS "CUDA_NVCC_FLAGS: ${CUDA_NVCC_FLAGS}")

# ----------------------------------------------------------------------------
# Create shared library project
# ----------------------------------------------------------------------------
add_library(${LIB_NAME} SHARED ${HEADERS} ${SOURCES} ${CUDA_SOURCES})
set(CUDA_LIBRARIES "cudadevrt.lib;cudart.lib")
target_link_libraries(${LIB_NAME} ${CUDA_LIBRARIES})

But it doesn't compile the cuda files with the right flags. Also in visual studio the preprocessor definitions are also in the cuda part of the properties....any suggestions?

Upvotes: 2

Views: 5411

Answers (1)

gflegar
gflegar

Reputation: 1613

I'll try to answer this question using the discussion from the comments and add some extra information.

First of all, there are two ways to enable CUDA support in CMake. One is the old FindCUDA module, and the other is the new built-in CUDA language support added in CMake 3.8, and explained here. You can choose one or the other (but you'll probably want to use the built-in support for new projects), but then you have to stick with your choice.

To use built-in support you either add it to the project(...) statement or use:

enable_language(CUDA);

To use the old FindCUDA package, you would use:

find_package(CUDA);

Note that the two options use completely different variables for setup. To see what variables are supported by FindCUDA see this page, and for built-in CUDA support see this (don't forget that the <LANG> placeholder can be replaced by any language, which means that CUDA is also one of the available substitutions).

E.g. with FindCUDA you would use CUDA_NVCC_FLAGS to set the compiler flags manually, and with built-in language support you would use CMAKE_CUDA_FLAGS for the same purpose. As a rule of thumb: if the variable starts with CUDA_ it is a part of the FindCUDA package, and if it starts with CMAKE_, then it is part of built-in support.

Upvotes: 10

Related Questions