CUDA: Incompatible System compiler is used for try_compile, even when compatible compiler was passed in CMAKE_CXX_COMPILER
Hello, I am using CMake 3.9.3 with Fedora 26.
The system compiler on Fedora 26 is GCC version 7.1, which is not compatible with any of the CUDA versions (latest: 9.0).
Luckily, I have prepared a compatible compiler in /opt/gcc/6.4.0/bin/g++
. I have passed it to CMake with
set(CMAKE_CXX_COMPILER /opt/gcc/6.4.0/bin/g++)
This, however is ignored by the try_compile
with CUDA. The CUDA try_compile
just calls which is nvcc without any additional flags, which causes nvcc to use the compiler it happens to find on the path. However, this one is the incompatible GCC version 7 compiler, and this causes try_compile
to fail and the configuration step to stop.
Workarounds are:
- Modify PATH variable to put the custom g++ on the PATH, before the system compiler
- Before calling cmake, set
CUDAHOSTCXX
environment variable - Turning a simple
project(myproject VERSION 1.0.0 LANGUAGES C CXX CUDA)
call into this:
project(myproject VERSION 1.0.0 LANGUAGES C CXX)
# When enabling the CUDA language, CMake uses the host compiler from PATH
# unless we specify the CUDAHOSTCXX environment variable
if ("$ENV{CUDAHOSTCXX}" STREQUAL "")
set(ENV{CUDAHOSTCXX} ${CMAKE_CXX_COMPILER})
endif()
enable_language(CUDA)
These Workarounds "work", but I highly suggest to change the logic in CMake to use the value provided in CMAKE_CXX_COMPILER
for the -ccbin
option.
For that matter, I think it's not enough to allow -ccbin
to be set only as an environment variable, I believe this should be settable by a distinct CMake variable.