CUDA: wrong compiler flags passed to host compiler
I have a project that requires using GCC 4.8.2. That ancient version doesn't understand "-std=c++14", but it does understand "-std=gnu++1y". When building C++ code, I can add the command "target_compile_features(test PUBLIC cxx_std_14)", and Cmake is smart enough to pass "-std=gnu++1y" to gcc. This is great.
Now I would like to add some CUDA code to this project. I'm using CMake 3.15, so I can just add "enable_language(CXX C CUDA)" and it will know to use nvcc to compile any .cu files. This is also great.
The problem is, when compiling the cu files, CMake appears to be passing "-std=c++14" to nvcc. Nvcc passes that along to the host compiler, which as we mentioned before, does not understand this flag. Nvcc issues a warning, and if there is any modern C++ code in the cu file, the build will fail. Here is my CMakeLists.txt:
project(test)
cmake_minimum_required(VERSION 3.15)
enable_language(CXX C CUDA)
add_executable(test cudatest.cu cpptest.cpp)
target_include_directories(test PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_features(test PUBLIC cxx_std_14)
set_target_properties(test PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
set_target_properties(test PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
And here is example output:
$ cmake -G Ninja ../cuda -- The C compiler identification is GNU 4.8.2 -- The CXX compiler identification is GNU 4.8.2 -- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/cc -- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/c++ -- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- The CUDA compiler identification is NVIDIA 10.1.243 -- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- works -- Detecting CUDA compiler ABI info -- Detecting CUDA compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /home//scratch/cudabuild $ cmake --build . -v [1/4] /opt/rh/devtoolset-2/root/usr/bin/c++ -I/home/me/scratch/cuda -std=gnu++1y -MD -MT CMakeFiles/test.dir/cpptest.cpp.o -MF CMakeFiles/test.dir/cpptest.cpp.o.d -o CMakeFiles/test.dir/cpptest.cpp.o -c /home/me/scratch/cuda/cpptest.cpp [2/4] /usr/local/cuda/bin/nvcc -I/home/me/scratch/cuda -std=c++14 -x cu -dc /home/me/scratch/cuda/cudatest.cu -o CMakeFiles/test.dir/cudatest.cu.o && /usr/local/cuda/bin/nvcc -I/home/me/scratch/cuda -std=c++14 -x cu -M /home/me/scratch/cuda/cudatest.cu -MT CMakeFiles/test.dir/cudatest.cu.o -o CMakeFiles/test.dir/cudatest.cu.o.d nvcc warning : The -std=c++14 flag is not supported with the configured host compiler. Flag will be ignored. nvcc warning : The -std=c++14 flag is not supported with the configured host compiler. Flag will be ignored. [3/4] /usr/local/cuda/bin/nvcc -Xcompiler=-fPIC -Wno-deprecated-gpu-targets -shared -dlink CMakeFiles/test.dir/cudatest.cu.o CMakeFiles/test.dir/cpptest.cpp.o -o CMakeFiles/test.dir/cmake_device_link.o -L/usr/local/cuda/targets/x86_64-linux/lib/stubs -L/usr/local/cuda/targets/x86_64-linux/lib -lcudadevrt -lcudart_static -lrt -lpthread -ldl [4/4] : && /opt/rh/devtoolset-2/root/usr/bin/c++ CMakeFiles/test.dir/cudatest.cu.o CMakeFiles/test.dir/cpptest.cpp.o CMakeFiles/test.dir/cmake_device_link.o -o test -L/usr/local/cuda/targets/x86_64-linux/lib/stubs -L/usr/local/cuda/targets/x86_64-linux/lib -lcudadevrt -lcudart_static -lrt -lpthread -ldl && :
The contents of cudatest.cu and cpptest.cpp are trivial, I can provide them if needed.