CUDA 8使用-std = gnu ++ 11编译错误 [英] CUDA 8 compile errors with -std=gnu++11
问题描述
我正在尝试将一些代码转换为使用CUDA,并且我认为我遇到兼容性问题。我们使用CMake。这些是我使用的gcc和CUDA版本:
I am trying to convert some code to use CUDA, and I think I am having compatibility issues. We use CMake. These are the gcc and CUDA versions that I use:
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61
在CMakeLists.txt文件中,设置为:
In the CMakeLists.txt file, this is set:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
编译时,出现此错误:
/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h(436): error: identifier "nullptr" is undefined
/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef.h(436): error: expected a ";"
/usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h(200): error: expected a ";"
/usr/include/c++/5/exception(63): error: expected a ";"
/usr/include/c++/5/exception(68): error: expected a ";"
/usr/include/c++/5/exception(76): error: expected a ";"
/usr/include/c++/5/exception(83): error: expected a ";"
/usr/include/c++/5/exception(93): error: expected a "{"
...
我发现此讨论,表示将C ++标志更改为 -std = c ++ 98
,但这不适用于该项目,因为它广泛使用了C ++ 11码。有解决方案吗?它似乎不是一个唯一的问题,但是我似乎找不到解决方法...
I found this discussion, which says to change the C++ flag to -std=c++98
, however this won't do for the project as it makes extensive use of C++11 code. Is there a solution for this? It doesn't appear to be an unique problem, but I can't seem to find a solution...
这是我运行<$ c $时的输出c> cmake ,所以我知道它确实在使用gcc 5.4,而不是其他版本。
This is the output when I run cmake
, so I know it is indeed using gcc 5.4 and not another version.
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /usr/local/cuda (found version "8.0")
-- Found OpenCV: /usr/local (found version "3.2.0")
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython2.7.so (found suitable version "2.7.12", minimum required is "2.7")
-- Found PythonInterp: /usr/bin/python2.7 (found version "2.7.12")
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- python
-- Configuring done
-- Generating
推荐答案
如果您想要更新版本的CMake,则更便携的解决方案是
If you are willing to require a more recent version of CMake, the more portable solution is
cmake_minimum_required(VERSION 3.1.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # not necessary, but encouraged
我大约90%确信它与< a href = https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html rel = nofollow noreferrer> v3.1.3
。确保在创建任何特定目标之前先设置(例如, cuda_add_library
或 cuda_add_executable
)。
I'm about 90% sure it came in with v3.1.3
. Make sure you set this before you create any specific targets (e.g., cuda_add_library
or cuda_add_executable
).
还请注意,如果您使用的是传统的 find_package(CUDA)
和 cuda_add_library
等,您可能还希望
Note also that if you are using traditional find_package(CUDA)
and cuda_add_library
etc, you will probably also want
set(CUDA_PROPAGATE_HOST_FLAGS ON)
否则, nvcc
默认情况下不会继承这些定义。
Otherwise nvcc
does not by default inherit these definitions.
这篇关于CUDA 8使用-std = gnu ++ 11编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!