使用CMake在NVCC中触发C ++ 11支持 [英] Triggering C++11 support in NVCC with CMake
问题描述
我正在运行具有CUDA 7.5的Ubuntu 15.10。 CMmake是v3.2.2,NVCC是7.5,v7.5.17; GCC是Ubuntu 5.2.1-22ubuntu2 v5.2.1
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
在常规项目中触发C ++ 11很容易:
Triggering C++11 in regular projects is easy with:
project(foo CXX)
set(TARGET foo CMAKE_CXX_STANDARD 11)
我正在使用以下命令定义CUDA项目:
I'm defining my CUDA project with:
find_package(CUDA REQUIRED)
CUDA_ADD_EXECUTABLE(foo ${foo_src} ${foo_hdr} ${foo_cu})
但是C ++ 11支持不会传播到NVCC。我必须添加:
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.
在将项目声明为C ++ 11时,如何使CMake自动设置C ++ 11标志?
How do I get CMake to automatically set the C++11 flags when declaring the project as C++11?
编辑:我已经使用CUDA 8.0和CMake 3.5.1。重新解决了这个问题。
I've retuned to to this question with CUDA 8.0 and CMake 3.5.1.
来自文档,设置(CUDA_PROPAGATE_HOST_FLAGS开启)
将传播 CMAKE_CXX_FLAGS
的内容,因此以下代码会同时为cpp和nvcc触发C ++ 11:
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)
但是, set(CMAKE_CXX_STANDARD 11)
不会影响 CMAKE_CXX_FLAGS
,因此以下给出了C ++ 11设备代码的编译器错误,因为没有东西可以传播:
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)
我似乎无法找到避免显式设置-std = c ++ 11 $ c $的CMake命令组合c>使用CXX或CUDA标志。
I can't seem to find a combination of CMake commands that avoids explicitly setting --std=c++11
in either CXX or CUDA flags.
推荐答案
自CMake 3.8起(由于CMake支持CUDA作为一种语言),有了一个新的目标属性 CUDA_STANDARD 。尽管它的名称很混乱,但它在 nvcc
编译命令中添加了 -std = XXX
。
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.
使用最新的CMake版本
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)
这篇关于使用CMake在NVCC中触发C ++ 11支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!