Triggering C++11 support in NVCC with CMake

2019-04-24 11:24发布

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.

标签: c++11 cuda cmake
1条回答
Melony?
2楼-- · 2019-04-24 12:02

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(3.8.2)
enable_language(CUDA)
add_executable(foo ${foo_src} ${foo_cu})
set_property(TARGET foo PROPERTY CUDA_STANDARD 11)
查看更多
登录 后发表回答