12
votes

I'm running Ubuntu 15.10 with CUDA 7.5. CMmake is v3.2.2, NVCC is release 7.5, v7.5.17; GCC is Ubuntu 5.2.1-22ubuntu2 v5.2.1

Triggering C++11 in regular projects is easy with:

project(foo CXX)
set(TARGET foo CMAKE_CXX_STANDARD 11)

I'm defining my CUDA project with:

find_package(CUDA REQUIRED)
CUDA_ADD_EXECUTABLE(foo ${foo_src} ${foo_hdr} ${foo_cu})

But the C++11 support doesn't get propagated to NVCC. I have to add:

list(APPEND CUDA_NVCC_FLAGS "-std=c++11")

This seems like a kludge. There was evidently work on this recently according to this task, but I haven't been able to find the results.

How do I get CMake to automatically set the C++11 flags when declaring the project as C++11?

EDIT: I've retuned to to this question with CUDA 8.0 and CMake 3.5.1.

From the documentation, set(CUDA_PROPAGATE_HOST_FLAGS ON) will propagate the contents of CMAKE_CXX_FLAGS, so the following triggers C++11 for both cpp and nvcc:

set (CMAKE_CXX_FLAGS "--std=c++11")
set (CUDA_PROPAGATE_HOST_FLAGS ON)

However, set(CMAKE_CXX_STANDARD 11) does not impact CMAKE_CXX_FLAGS, so the following gives compiler errors for C++11 device code, as there's nothing to propagate:

set (CMAKE_CXX_STANDARD 11)
set (CUDA_PROPAGATE_HOST_FLAGS ON)

I can't seem to find a combination of CMake commands that avoids explicitly setting --std=c++11 in either CXX or CUDA flags.

1
try setting CUDA_PROPAGATE_HOST_FLAGS to ON (although it is suposed to be ON by default) and then set CMAKE_CXX_FLAGS with -std=c++11.Hopobcn
Tried setting CUDA_PROPAGATE_HOST_FLAGS to ON. Didn't work. The command set(TARGET foo CMAKE_CXX_STANDARD 11) already triggers C++11 support in the compiler. Seems redundant to add set(CMAKE_CXX_FLAGS "-std=c++11") on top of that.Andreas Yankopolus
I think this functionality depends on your cmake version. In my case with cmake 3.5.0 setting CMAKE_CXX_FLAGS with -std=c++11 and having CUDA_PROPAGATE_HOST_FLAGS automatically triggers cmake to add --std c++11 (to nvcc).Hopobcn
I updated to CMake 3.5.1 and have been looking at FindCUDA.cmake. I'm printing the value of CMAKE_CXX_FLAGS after setting CMAKE_CXX_STANDARD to 11 and am surprised that it's empty. Am I wrong to think that CMake should populate it based on the value of CMAKE_CXX_STANDARD and other variables?Andreas Yankopolus
@AndreasYankopolus The same problems. CMake 3.10.0 and CUDA 9.0.chaosink

1 Answers

6
votes

Since CMake 3.8 (since CMake supports CUDA as a language) there is a new target property CUDA_STANDARD. Although its name is quite confusing, it adds the -std=XXX to the nvcc compile command.

With a recent CMake version the proper way would be

cmake_minimum_required(VERSION 3.8.2)
enable_language(CUDA)
add_executable(foo ${foo_src} ${foo_cu})
set_property(TARGET foo PROPERTY CUDA_STANDARD 11)