c – CMDA的CUDA编译问题

前端之家收集整理的这篇文章主要介绍了c – CMDA的CUDA编译问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有使用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 verbose进行编译为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(),op);
}

main.cpp中

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

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

猜你在找的C&C++相关文章