3

Is there a CMake command that can be used to set the Cuda runtime library (in visual studio under Cuda C/C++ -> Host -> Runtime Library)? For instance I would need to set it to MTd rather than MDd in debug mode, and MT rather than MD in release mode. I can do this manually in visual studio after building, but it would be great to have this be automatic using my CMakeLists.txt file. Thanks!

P.S. I know how to set this for the normal runtime library (not Cuda); this question is specific to the Cuda runtime library.

UPDATE 2022-01-24: The helpful input has lead me to the CMake variable CMAKE_CUDA_FLAGS_RELEASE which seems to control this. I need to set it like so: set(CMAKE_CUDA_FLAGS_RELEASE " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")

But unfortunately it seems to get overwritten for some reason, back to -MD.

UPDATE 2022-01-25: Example code:

cmake_minimum_required(VERSION 3.8)

#set(CMAKE_CUDA_RUNTIME_LIBRARY Shared)
#string(APPEND CMAKE_CUDA_FLAGS_RELEASE_INIT " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")
#set(CMAKE_CUDA_FLAGS_RELEASE_INIT " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")
set(CMAKE_CUDA_FLAGS_RELEASE " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")

project(cuda_test_integrated LANGUAGES CUDA CXX)

# multi-threaded
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")

find_package(CUDAToolkit REQUIRED)
set(CMAKE_CUDA_ARCHITECTURES OFF)

set(CMAKE_CUDA_FLAGS_RELEASE " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")


# modules
include_directories("../cuda_module_example")
add_subdirectory("../cuda_module_example" "${CMAKE_CURRENT_BINARY_DIR}/cuda_module_example")


include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
add_executable(cuda_test_integrated main.cxx)

set(CMAKE_CUDA_FLAGS_RELEASE " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")

set_target_properties(cuda_test_integrated PROPERTIES CUDA_SEPARABLE_COMPILATION ON)

target_link_libraries(cuda_test_integrated CUDA::cudart)
target_link_libraries(cuda_test_integrated cuda_module_example)

set(CMAKE_CUDA_FLAGS_RELEASE " -Xcompiler=\"-MT -O2 -Ob2\" -DNDEBUG")
mikewho
  • 125
  • 1
  • 7
  • 1
    You would have to rebuild all dependent libraries with the same runtime provided you have the sources. This CMake documentation should help: [https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html](https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html) – drescherjm Jan 17 '22 at 18:27
  • Also this answer: [https://stackoverflow.com/a/56776146/487892](https://stackoverflow.com/a/56776146/487892) – drescherjm Jan 17 '22 at 18:33
  • Your answer seems to be for non-cuda runtime library setting. I am asking specifically for the Cuda toolkit in Visual Studio. If you right click on a project in Visual Studio, and go to Cuda C/C++ -> Host -> Runtime Library, I just need to be able to set that value using CMake. All of my projects already have the correct (non-Cuda) runtime library, I just need to make the Cuda one match them. – mikewho Jan 17 '22 at 19:18
  • 2
    Maybe? https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_RUNTIME_LIBRARY.html and https://stackoverflow.com/questions/48527238/linking-of-cuda-library-in-cmake – InUser Jan 23 '22 at 11:39
  • This has brought me closer I think. While these flags specifically don't help, the flag CMAKE_CUDA_FLAGS_RELEASE is what I need to set. Setting it manually to -MT after doing "Configure" in CMake does exactly what I need, now I just need to figure out how to set this with a CMake command. Unfortunately a straight SET doesn't seem to work for some reason, it gets overwritten back to -MD. – mikewho Jan 24 '22 at 21:05
  • Maybe it's overwritten somewhere or you put it in the wrong place (not under the correct config for example) – InUser Jan 25 '22 at 07:44
  • I've added some example code where I've put the set command in various places, but it still seems to be overwritten. Let me know if there's something I'm missing. – mikewho Jan 25 '22 at 17:46

0 Answers0