How to correctly collect a CUDA C++ project using Cmake on Linux?

Fails to properly write Cmake to produce build the project. Crashes error undefined reference to `main'.
Project structure:
main.cpp
include
-- CUDA_wrappers.hpp
src
-- CUDA.cu
cmake_minimum_required(VERSION 3.8)
project(Mandelbrot_set)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp include/CUDA_wrappers.hpp src/CUDA.cu)

# LIBGD Package
find_package(GD REQUIRED)
include_directories(${GD_INCLUDE_DIR})

# CUDA Package
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})

# Compile CU files
list( APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_20,code=sm_20; -std=c++11")
cuda_compile(CudaKernel src/CUDA.cu)
cuda_add_library(CudaKernels src/CUDA.cu)

# COMPILE AND LINK
cuda_add_executable(Mandelbrot_set $(SOURCE_FILES) CudaKernels)
target_link_libraries(Mandelbrot_set ${GD_LIBRARIES} ${CUDA_LIBRARIES})

In Cmake do not understand, I tried different options from the network.
CUDA sort works correctly, it sees my adapter, and correctly displays its characteristics.
In main.cpp and CUDA.cu included CUDA_wrappers.hpp. In CUDA_wrappers.hpp is declared a function that is defined in CUDA.cu. This function is called in main.cpp
If you need more details - misleading.
June 8th 19 at 17:21
1 answer
June 8th 19 at 17:23
Solution
cuda_compile(CudaKernel src/CUDA.cu)
cuda_add_library(CudaKernels src/CUDA.cu)

Suspiciously different identifiers, it is exactly necessary? Why is this cuda_compile? If you use it, then you have the fact that he put in CudaKernel to substitute in cuda_add_library, for example cuda_add_library(CudaKernels ${CudaKernel})

cuda_add_executable(Mandelbrot_set $(SOURCE_FILES) CudaKernels)

A strange design. If CudaKernels is the target, what is he doing in this list, which should be the source?

And whether src/CUDA.cu kompilacja twice?
It is possible that I ponahvatali all from different sources and therefore what I wrote makes no sense. I would understand how to rewrite it so that worked. - Gene commented on June 8th 19 at 17:26
try this part:
cuda_compile(CudaKernel src/CUDA.cu)
cuda_add_library(CudaKernels src/CUDA.cu)

# COMPILE AND LINK
cuda_add_executable(Mandelbrot_set $(SOURCE_FILES) CudaKernels)
target_link_libraries(Mandelbrot_set ${GD_LIBRARIES} ${CUDA_LIBRARIES})


Replace with the following:
cuda_add_library(CudaKernels src/CUDA.cu)

# COMPILE AND LINK
cuda_add_executable(Mandelbrot_set ${SOURCE_FILES})
target_link_libraries(Mandelbrot_set CudaKernels ${GD_LIBRARIES} ${CUDA_LIBRARIES})

And if there are errors, please describe them in detail. - Breana_Leffl commented on June 8th 19 at 17:29
cmake_minimum_required(VERSION 3.8)
project(Mandelbrot_set)

set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
set(CUDA_PROPAGATE_HOST_FLAGS OFF)

include_directories(include)

# LIBGD Package
find_package(GD REQUIRED)
include_directories(${GD_INCLUDE_DIR})

# CUDA Package
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})

# Compile CU files
list( APPEND CUDA_NVCC_FLAGS "-gencode arch=compute_30,code=sm_30; -std=c++11")
cuda_compile(CudaKernel src/CUDA.cu)

# COMPILE AND LINK
add_executable(Mandelbrot_set ${SOURCE_FILES} ${CudaKernel})
target_link_libraries(Mandelbrot_set ${GD_LIBRARIES} ${CUDA_LIBRARIES})

So the project seems to be gathered somehow. However when run crashes segmentation fault when calling the function, which is declared in CUDA_wrappers.hpp. It seems that the problem isn't solved. Well, at least it compiles CUDA code.
In the way that you suggest the comment above, the project also successfully gathered. But the problem is the same, segmentation fault when accessing function from CUDA_wrappers.hpp. - Gene commented on June 8th 19 at 17:32

Find more questions by tags CMakeLinuxCUDAC++