Reputation: 149
I am trying to add CUDA functions in existing C++ project which uses CMake.
For example, main.cpp
looks like this:
#include <stdio.h>
#include "kernels/test.cuh"
int main() {
wrap_test_print();
return 0;
}
And kernels/test.cu
looks like:
#include "test.cuh"
__global__ void test_print(){
printf("Hello World!\n");
}
void wrap_test_print() {
test_print<<<1, 1>>>();
return;
}
And kernels/test.cuh
looks like:
#ifndef TEST_CUH__
#define TEST_CUH__
#include <stdio.h>
void wrap_test_print();
#endif
And I use following codes for CMakeLists.txt
:
===============
CMakeLists.txt
===============
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
enable_language(CUDA)
project(cmake_and_cuda)
add_executable(main main.cpp)
add_subdirectory(kernels)
# set_property(TARGET main
# PROPERTY CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(main kernels)
===============
kernels/CMakeLists.txt
===============
enable_language(CUDA)
add_library(kernels
test.cu
test.cuh
)
target_compile_features(kernels PUBLIC cxx_std_11)
set_target_properties(kernels
PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(kernels)
However, when I use cmake ..
in the build
folder in the project, the following error message are printed:
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_CUDA_DEVICE_LINK_LIBRARY
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
CMAKE_CUDA_DEVICE_LINK_LIBRARY
I guess this is due to a cudart
problem, so I added set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lcudart")
but I could not resolve this issue. How can I solve this issue?
Upvotes: 8
Views: 12124
Reputation: 18353
Turning my comment into an answer:
The project()
command will initialize many CMake variables concerning your system and compiler. As such, it sets the languages that your CMake project will be using. Without specifying any language in the project()
command, the defaults (C
and CXX
) are enabled:
# Initialize for C and C++ languages.
project(cmake_and_cuda)
You should typically place the first project()
command directly after the cmake_minimum_required()
call, to avoid such errors. The enable_language()
is a light-weight call, typically used to enable further languages later on in the CMake project. So, you should re-arrange your CMake file to place the enable_language()
call after the project()
call, so that CMake has initialized its internal variables properly.
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
# Initialize for C and C++ languages.
project(cmake_and_cuda)
# Also, enable CUDA language support.
enable_language(CUDA)
Or, simply enable all the languages your CMake project needs in the project()
command itself:
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
# Initialize for C++ and CUDA languages.
project(cmake_and_cuda LANGUAGES CXX CUDA)
Note: The enable_language()
and project()
command's languages apply to all CMake directories below them, so there is no need to call enable_language()
again in sub-directories.
Upvotes: 8