C++ CMake and NVCC compilation using Thrust

I have an issue while trying to use CMAKE with my CUDA project. I use the following CMakeLists.txt file:

# ------ Includes 
include_directories("${CMAKE_SOURCE_DIR}/include/")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/")

# ------ Cuda 
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
link_directories(${CUDA_LIBRARY_DIRS})
set(CUDA_SEPARABLE_COMPILATION ON)
set(CUDA_ARCH -gencode arch=compute_35,code=sm_35)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} ${CUDA_ARCH} -g -G --std=c++11)

# ------ Thrust
find_package(Thrust REQUIRED)

# ------ OpenMP support required 
find_package(OpenMP REQUIRED)
include_directories(${OPENMP_INCLUDE_DIRS})
link_directories(${OPENMP_LIBRARY_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

# ------ HDF5 library
find_package(HDF5)
include_directories(${HDF5_INCLUDE_DIR})
link_directories(${HDF5_LIBRARY_DIR})

# ------ Debug or Release 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -pg")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

cuda_add_executable(TEST truc.cu [ CU files list] machin.cu)
target_link_libraries(TEST hdf5)

Using it, my code compile successfully. Then I added in one of my files a sort function using Thrust :

#include <thrust/device_ptr.h>
#include <thrust/sort.h>
...
void sort_arrays(tree_node_t * array, uint nElems){
    thrust::device_ptr<tree_node_t> array_ptr = thrust::device_pointer_cast(array);
    thrust::sort(array_ptr,array_ptr+nElems,sort_tree_node_t); 
}
...

Then I get a linking error:

[ 10%] Building NVCC (Device) object src/CMakeFiles/TEST.dir/TEST_generated_test.cu.o
....
[ 90%] Building NVCC intermediate link file src/CMakeFiles/TEST.dir/TEST_intermediate_link.o
Scanning dependencies of target TEST
[100%] Linking CXX executable TEST
CMakeFiles/TEST.dir/TEST_intermediate_link.o: In function `__cudaRegisterLinkedBinary_66_tmpxft_00000df5_00000000_17_cuda_device_runtime_compute_61_cpp1_ii_8b1a5d37':
/tmp/tmpxft_000045e7_00000000-2_TEST_intermediate_link.reg.c:10: undefined reference to `__fatbinwrap_66_tmpxft_00000df5_00000000_17_cuda_device_runtime_compute_61_cpp1_ii_8b1a5d37'
collect2: error: ld returned 1 exit status
make[2]: *** [src/TEST] Error 1
make[1]: *** [src/CMakeFiles/TEST.dir/all] Error 2
make: *** [all] Error 2

Note that I get this error before adding the thrust code but adding right compilation setting to the CMakeLists solved it :

set(CUDA_SEPARABLE_COMPILATION ON)

cross-posted:

[url]http://stackoverflow.com/questions/43047495/c-cmake-and-nvcc-compilation-using-thrust[/url]

I had no idea there was a FindThrust.

But basically, if you need separable compilation, you need separable compilation :P

Just google it and read up more on it to find out why you need it.