I am developing a static C++ library using MS Visual Studio 2022.
I am also using CUDA nvcc compiler to buld .cu files which are part of the code base.
When generating VS project from CMake I am receiving the following error:
nvcc fatal : A single input file is required for a non-link phase
when an outputfile is specified
Since I have also a version of this project configured manually I started comparing “CUDA C/C++” section of the project properties.
The difference I see is that Cmake adds into Compiler command line additional options window the following:
%(AdditionalOptions) -std=c++17 -arch=all-major /GR- /W4 -Xcompiler="/EHsc -Zi -Ob0"
Once I remove everything from “Additional Options” window the compilation succeeds. I can’t find a way to cause Cmake not to add any extra options.
I tried to clear CUDA compiler options explicitly:
target_compile_options(MyCudaLib PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler="">
)
Also tried this:
set(CMAKE_CUDA_FLAGS "")
But it changed nothing.
I also can see that CUDA linker gets
-forward-unknown-to-host-compiler -Wno-deprecated-gpu-targets -Xcompiler=" -Zi -Ob0 -Od /RTC1" -arch=all-major -Xcompiler=-MDd
into “additional options” as well. So my question is how to configure Cmake so that it doesn’t add any extras into compiler and linker options. Also,how to prevent Cmake from adding same includes and preprocessor definitions into CUDA C/C++ compiler settings as set(CUDA_PROPAGATE_HOST_FLAGS OFF) seems not to have any effect.
Here are my Cmake files:
Root Cmake:
cmake_minimum_required(VERSION 3.28)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.23.0" AND NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES all-major)
endif()
project(TestProject LANGUAGES CXX C CUDA)
# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
set(CMAKE_CUDA_SEPARABLE_COMPILATION ON)
add_definitions(-D_HAS_EXCEPTIONS=0)
add_compile_definitions(
$<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:Release>:NDEBUG>
)
add_compile_options(
/GR- # Disable RTTI
/W4 # Set warning level 4
)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /Zi") # Debug symbols and disable optimizations
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /DNDEBUG") # Optimized build
string(REGEX REPLACE "/EH[a-z]+" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
add_subdirectory(MyCudaLib)
MyCudaLib CMake:
add_library(MyCudaLib STATIC)
set_target_properties(MyCudaLib PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_include_directories(MyCudaLib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
)
set_target_properties(MyCudaLib PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
set_target_properties(MyCudaLib PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(MyCudaLib PROPERTIES CUDA_LINK_OPTIONS "-dlink")
# Header files
file(GLOB_RECURSE MyCudaLib_HEADERS "include/*.h")
target_sources(MyCudaLib PRIVATE ${MyCudaLib_HEADERS})
source_group("Headers" FILES ${MyCudaLib_HEADERS})
set(CPP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/SomeCppStuff.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/MyKernels.cu
)
target_sources(MyCudaLib PRIVATE ${CPP_SOURCES})
target_include_directories(MyCudaLib PRIVATE ${XXX_DIR}/include)
# Clear CUDA compile flags for a specific target (no effect)
target_compile_options(MyCudaLib PRIVATE
$<$<COMPILE_LANGUAGE:CUDA>:> )
