Compilation error when using target_compile_definitions with cu files on VS2017, CUDA 9.0.
I am on Windows 10, CMake 3.9.4. I recently upgraded from Visual Studio 2015, CUDA 8 to Visual Studio 2017, CUDA 9 and run into a problem that I didn't have when using VS2015 and CUDA 8.
I was following @robertmaynard post at https://devblogs.nvidia.com/parallelforall/building-cuda-applications-cmake/ to update my CMakeLists but here is the problem. Suppose I have the below CMakeLists
cmake_minimum_required(VERSION 3.9)
project(Example LANGUAGES CXX CUDA)
if(MSVC)
set(DISABLE "__pragma(warning(push,0))")
set(ENABLE "__pragma(warning(pop))")
endif()
set(PROJECT_SRCS ${PROJECT_SOURCE_DIR}/main.cu)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_compile_definitions(${PROJECT_NAME} PUBLIC
WINDOWS_DISABLE_ALL_WARNING=${DISABLE}
WINDOWS_ENABLE_ALL_WARNING=${ENABLE})
and my main.cu is as simple as
WINDOWS_DISABLE_ALL_WARNING
#include <vector>
WINDOWS_ENABLE_ALL_WARNING
int main(){
return 0;
}
When I compile the code, I get the below error
Build FAILED.
"C:\Users\jasju\Desktop\test\build\Example.sln" (default target) (1) ->
"C:\Users\jasju\Desktop\test\build\Example.vcxproj.metaproj" (default target) (2) ->
"C:\Users\jasju\Desktop\test\build\Example.vcxproj" (default target) (4) ->
"C:\Users\jasju\Desktop\test\build\Example.vcxproj" (CudaBuildCore target) (4:2) ->
(CudaBuildCore target) ->
C:/Users/jasju/Desktop/test/main.cu(9): fatal error C1001: An internal error has occurred in the compiler. [C:\Users\
jasju\Desktop\test\build\Example.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\BuildCustomizations\CUDA 9.0.t
argets(707,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin\nvcc.exe" -genc
ode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env --cl-version 2017 -ccbin "C:\Program Files (x86)\Microsof
t Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\bin\HostX86\x64" -x cu -I"C:\Program Files\NVIDIA GPU Computi
ng Toolkit\CUDA\v9.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include" --keep-dir x64\Re
lease -maxrregcount=0 --machine 64 --compile -cudart static -Xcompiler="/EHsc -Ob2" -D_WINDOWS -DNDEBUG -D"WINDOWS_
DISABLE_ALL_WARNING=__pragma(warning(push,0))" -D"WINDOWS_ENABLE_ALL_WARNING=__pragma(warning(pop))" -D"CMAKE_INTDIR=\"
Release\"" -D"WINDOWS_DISABLE_ALL_WARNING=__pragma(warning(push,0))" -D"WINDOWS_ENABLE_ALL_WARNING=__pragma(warning(pop
))" -D"CMAKE_INTDIR=\"Release\"" -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /FS /Zi /MD /GR" -o Example.dir\Release\mai
n.cu.obj "C:\Users\jasju\Desktop\test\main.cu"" exited with code 2. [C:\Users\jasju\Desktop\test\build\Example.vcxproj]
0 Warning(s)
2 Error(s)
However, if I change main.cu into main.cpp, everything compiles just fine
CMakeLists
cmake_minimum_required(VERSION 3.9)
project(Example)
if(MSVC)
set(DISABLE "__pragma(warning(push,0))")
set(ENABLE "__pragma(warning(pop))")
endif()
set(PROJECT_SRCS ${PROJECT_SOURCE_DIR}/main.cpp)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_compile_definitions(${PROJECT_NAME} PUBLIC
WINDOWS_DISABLE_ALL_WARNING=${DISABLE}
WINDOWS_ENABLE_ALL_WARNING=${ENABLE})
main.cpp
WINDOWS_DISABLE_ALL_WARNING
#include <vector>
WINDOWS_ENABLE_ALL_WARNING
int main(){
return 0;
}
Another thing is if I don't use target_compile_definitions
and directly uses it like the below it also compiles
CMakeLists
cmake_minimum_required(VERSION 3.9)
project(Example LANGUAGES CXX CUDA)
set(PROJECT_SRCS ${PROJECT_SOURCE_DIR}/main.cu)
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
main.cu
__pragma(warning(push,0))
#include <vector>
__pragma(warning(pop))
int main(){
return 0;
}
But this approach is not flexible enough because I would need to have a macro to redefine for linux otherwise the pragma warning keywords will cause compilation errors in GCC.
Why is it that the combination of target_compile_definitions
and cu files will cause the compilation error?