Problem with setting compilation flags for a library that has both C++ and CUDA source files with modern CMake approach.
I am on Windows 10, CUDA 9, VS2017, CMake 3.9.5.
Suppose I have mycpp.cpp
and mycu.cu
in my library, and I want to set compilation flags without touching CMAKE_CXX_FLAGS
, because according to @purpleKarrot in https://www.slideshare.net/DanielPfeifer1 slide 29 we shouldn't, and we should use target_compile_options
instead. Here is my below CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(Example LANGUAGES CXX CUDA)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/mycpp.cpp
${PROJECT_SOURCE_DIR}/mycu.cu)
add_library(${PROJECT_NAME} ${PROJECT_SRCS})
target_compile_options(${PROJECT_NAME} PUBLIC /W4)
This returns compilation error because NVCC does not recognize /W4
. If I update it to
target_compile_options(${PROJECT_NAME} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:/W4>)
It returns cmake error
Error evaluating generator expression:
$<COMPILE_LANGUAGE:CXX>
$<COMPILE_LANGUAGE:...> may not be used with Visual Studio generators.
The only combination that I managed to get it to work is
cmake_minimum_required(VERSION 3.9)
project(Example LANGUAGES CXX CUDA)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/mycpp.cpp
${PROJECT_SOURCE_DIR}/mycu.cu)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
add_library(${PROJECT_NAME} ${PROJECT_SRCS})
Is there a way to make it work by using target_compile_options
?