-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Description
When linking spdlog::spdlog_header_only against a cmake target that contains .cu files and it is built with the MSVC c++ compiler as the host compiler, the flag /Zc:__cplusplus is passed as is to the cuda compiler causing the command to compile the cuda files to be malformed.
Example:
find_package(spdlog REQUIRED)
#...
add_executable(main main.cu)
target_link_libraries(main PRIVATE spdlog::spdlog_header_only)which may create the following compile command
C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\bin\nvcc.exe -forward-unknown-to-host-compiler -DFMT_SHARED -DSPDLOG_FMT_EXTERNAL -DSPDLOG_FWRITE_UNLOCKED -isystem C:\dev\vcpkg\installed\x64-windows\include -D_WINDOWS -Xcompiler=" /EHsc" -Xcompiler=" -Zi -Ob0 -Od /RTC1" -std=c++14 "--generate-code=arch=compute_52,code=[compute_52,sm_52]" -Xcompiler=-MDd /Zc:__cplusplus -MD -MT CMakeFiles\main.dir\main.cu.obj -MF CMakeFiles\main.dir\main.cu.obj.d -x cu -c C:\dev\cpp\cflagtest\main.cu -o CMakeFiles\main.dir\main.cu.obj -Xcompiler=-FdCMakeFiles\main.dir\,-FSProblem
This is caused by target_compile_options(spdlog_header_only INTERFACE "/Zc:__cplusplus"). To pass the flag correctly to the host compiler it must be prefaced by -Xcompiler.
Fix
So the suggested fix is to add the flag through cmake generator expressions differently when compiling c++ and cuda code:
target_compile_options(spdlog_header_only INTERFACE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/Zc:__cplusplus>
$<$<AND:$<COMPILE_LANGUAGE:CUDA>,$<CXX_COMPILER_ID:MSVC>>:-Xcompiler=/Zc:__cplusplus>)Workaround
Without modifying the original installed cmake files from spdlog, one can modify the flags after importing the package in cmake:
get_target_property(spdlogflags spdlog::spdlog_header_only INTERFACE_COMPILE_OPTIONS)
string(REPLACE "/Zc:__cplusplus" "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/Zc:__cplusplus>$<$<AND:$<COMPILE_LANGUAGE:CUDA>,$<CXX_COMPILER_ID:MSVC>>:-Xcompiler=/Zc:__cplusplus>" newspdlogflags ${spdlogflags})
set_target_properties(spdlog::spdlog_header_only PROPERTIES INTERFACE_COMPILE_OPTIONS ${newspdlogflags})