c++ - CMake 的 CUDA 编译问题

标签 c++ c++11 cuda cmake

我在使用 CMake 编译我的 CUDA 代码时遇到问题。我使用的是 CUDA 7,来自 nvcc 的版本信息如下:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2014 NVIDIA Corporation
Built on Tue_Dec__9_18:10:46_CST_2014
Cuda compilation tools, release 7.0, V7.0.17

我的 CMake 文件使用 find_cuda 宏,如下所示:

find_package(CUDA)
if(CUDA_FOUND)
    list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;--compiler-options;-std=c++11;-O2;-DVERBOSE")
endif(CUDA_FOUND)

在许多帖子表明这是必需的之后,我添加了 std=c++11 编译器标志。但是,无论有没有这个标志,我都会得到完全相同的错误。

我还添加了以下内容以从 nvcc 编译标志中删除 C++11 支持,但这也不会改变任何内容。

if(CMAKE_COMPILER_IS_GNUCC)
    string(REPLACE "-std=c++11" "" CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}")
    string(REPLACE "-std=c++0x" "" CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCC)

我得到的错误如下:

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected
a ";"

/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h(190): error: 
expected a ";"

/usr/include/c++/4.8/exception(63): error: expected a ";"

/usr/include/c++/4.8/exception(68): error: expected a ";"

/usr/include/c++/4.8/exception(76): error: expected a ";"

/usr/include/c++/4.8/exception(83): error: expected a ";"

/usr/include/c++/4.8/exception(93): error: expected a "{"

/usr/include/c++/4.8/bits/exception_ptr.h(64): error: function 
"std::current_exception" returns incomplete type  
"std::__exception_ptr::exception_ptr"

我使用的是 gcc 4.8,但在 4.7 中也遇到了同样的错误。我在 cmake 2.8.12.2 上。

使用 CMAKE 详细编译会为 nvcc 编译提供以下标志:

/usr/local/cuda-7.0/bin/nvcc /home/xargon/Dropbox/code/gpu-mosaicing
/src/gpu/kernels/bgra_2_gray.cu -c -o /home/xargon/code/mosaicing_bin
/gpu/kernels/CMakeFiles/kernels.dir//./kernels_generated_bgra_2_gray.cu.o 
-ccbin /usr/bin/cc -m64 -DUSE_CUDA -DUSE_OPENCV -DUSE_QT -Xcompiler 
,\"-std=c++11\",\"-O3\",\"-DNDEBUG\" -arch=sm_20 --compiler-options 
-std=c++11 -O2 -DVERBOSE -DNVCC -I/usr/local/cuda-7.0/include -I/usr/local
/include/opencv -I/usr/local/include -I/home/xargon/Dropbox/code/gpu-
mosaicing/src/cpu/gui/qt -I/usr/include -I/home/xargon/Dropbox/code/gpu-
mosaicing/src/cpu/core -I/home/xargon/Dropbox/code/gpu-mosaicing/src/cpu
/datasources -I/home/xargon/Dropbox/code/gpu-mosaicing/src/gpu
/intraoperability -I/home/xargon/Dropbox/code/gpu-mosaicing/src/utils 
-I/usr/local/cuda-7.0/include

最佳答案

这对我使用 CUDA 7、gcc 4.8.2 和 CMake 3.0.2 有效。

我更新了代码并添加了一个简单的基于推力的示例,以明确您可以在 CUDA 代码中使用 C++11

CMakeLists.txt

project(cpp11)
find_package(CUDA)
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;-std=c++11;-O2;-DVERBOSE")
SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
CUDA_ADD_EXECUTABLE(cpp11 main.cpp test.h test.cu)

test.h

#ifndef TEST_H
#define TEST_H
int run();
#endif

test.cu

#include "test.h"
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/sequence.h>

template<typename T>
struct Fun
{
        __device__ T operator()(T t1, T t2)
        {
            auto result = t1+t2;
            return result;
        }
};

int run()
{
    const int N = 100;
    thrust::device_vector<int> vec(N);
    thrust::sequence(vec.begin(),vec.end());
    auto op = Fun<int>();
    return thrust::reduce(vec.begin(),vec.end(),0,op);
}

ma​​in.cpp

#include <iostream>
#include "test.h"

int main()
{
    std::cout << run() << std::endl;
    return 0;
}

关于c++ - CMake 的 CUDA 编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29121211/

相关文章:

c++ - 使用 CUDA Driver API VS2012 时未解析的外部符号

c++ - 将 CUDA printf 重定向到 C++ 流

c - 为什么我的c程序突然用了30g的虚拟内存?

c++ - 多重声明与未定义

c++ - 奈奎斯特频率限制 - 如何仅混合低于奈奎斯特限制的谐波

c++ - 设备驱动程序库

c++ - 如何使用 recv() 在 C++ 套接字中接收超过 65000 个字节

c++ - 具有某些公共(public)成员的强制转换结构

c++ - 为什么 is_swappable 和 is_nothrow_swappable 不包含在 C++11 中?

c++ - 如何让虚拟析构函数在 C++ 中被调用?