D
D
Dmitry Bunin2018-01-31 18:58:32
linux
Dmitry Bunin, 2018-01-31 18:58:32

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

It is not possible to write Cmake correctly so that it is possible to build the project. An undefined reference to `main' error is thrown.
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})

I do not understand Cmake, I tried various options from the network.
CUDA seems to work correctly, it sees my adapter and correctly displays its characteristics.
CUDA_wrappers.hpp is included in main.cpp and CUDA.cu. A function is declared in CUDA_wrappers.hpp which is defined in CUDA.cu. This function is called in main.cpp
If more details are needed, I'll let you know.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2018-01-31
@Mr-Geekman

cuda_compile(CudaKernel src/CUDA.cu)
cuda_add_library(CudaKernels src/CUDA.cu)

Suspiciously different identifiers, is this exactly how it should be? Why this cuda_compile at all? If you use it, then you need to substitute what it puts in CudaKernel into cuda_add_library, for examplecuda_add_library(CudaKernels ${CudaKernel})
Strange construction. If CudaKernels is a target, what does it do in this list, where should the sources be?
And should src/CUDA.cu be compiled twice?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question