Luca Luca - 3 months ago 104
C++ Question

CUDA compilation issue with CMake

I am having issues with compiling my CUDA code with CMake. I am using CUDA 7 and the version information from nvcc is as follows:

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


My CMake file uses the find_cuda macro as follows:

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


I added the std=c++11 compiler flag after many posts suggested this was needed. However, I get exactly the same errors with or without this flag.

I also added the following to remove the C++11 support from nvcc compilation flags but this does not change anything either.

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)


The errors I get are as follows:

/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"


I am using gcc 4.8 but get the same errors with 4.7 as well. I am on cmake 2.8.12.2.

Compiling with CMAKE verbose gives the following flags for nvcc compilation:

/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

Answer

This worked for me using CUDA 7, gcc 4.8.2 and CMake 3.0.2.

I updated the code and added a simple thrust-based example to make it clear that you can use C++11 in CUDA code

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);
}

main.cpp

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

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