I want to create a skeleton for a project in which there are multiple cuda and cpp files. They will be compiled individually and then linked together to form a single executable. Currently I have the following:
- main.cpp
- host.cpp
- host.h
- device.cu
- device.cuh
- CMakeLists.txt
the source files contain the following (the header files only declare the functions implemented in host.cpp and device.cu):
// main.cpp
#include "host.h"
#include "device.cuh"
int main()
{
a();
b();
return 0;
}
// host.cpp
#include <iostream>
#include "host.h"
int b() {
std::cout << "B" << std::endl;
return 0;
}
// device.cu
#include <iostream>
#include "device.cuh"
int a()
{
std::cout << "A" << std::endl;
return 0;
}
# cmake script
cmake_minimum_required(VERSION 3.28)
set(CMAKE_CUDA_COMPILER /usr/local/cuda-13.0/bin/nvcc)
project(cuda_project LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_ARCHITECTURES 86)
file(GLOB_RECURSE SOURCES
${CMAKE_SOURCE_DIR}/*.cpp
${CMAKE_SOURCE_DIR}/*.cu
)
add_executable(${PROJECT_NAME} ${SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
when executing the cmake (using CLion ide btw) I get the error:
====================[ Build | cuda_project | Debug ]============================
/home/yonathan/Desktop/clion-2025.2/bin/cmake/linux/x64/bin/cmake --build /home/yonathan/code/cuda-final/cmake-build-debug --target cuda_project -j 10
[1/3] Building CXX object CMakeFiles/cuda_project.dir/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp.o
FAILED: CMakeFiles/cuda_project.dir/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp.o
/usr/bin/c++ -g -std=gnu++23 -fdiagnostics-color=always -MD -MT CMakeFiles/cuda_project.dir/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp.o -MF CMakeFiles/cuda_project.dir/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp.o.d -o CMakeFiles/cuda_project.dir/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp.o -c /home/yonathan/code/cuda-final/cmake-build-debug/CMakeFiles/4.0.2/CompilerIdCUDA/tmp/CMakeCUDACompilerId.cudafe1.cpp
/usr/lib/gcc/x86_64-linux-gnu/13/include/stddef.h:218:10: fatal error: crt/host_runtime.h: No such file or directory
218 | #endif /* !(defined (__GNUG__) && defined (size_t)) */
| ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
From what the error suggests and from what I understood online the CMakeCUDACompilerId object is used to "test" the nvcc before compiling the source code or something similar. However, for some reason it isn't used and c++ does (what even is c++? isn't the cpp compiler named g++?) how do I fix this problem?
Additional info that might prove relevant to the question: I'm using linux mint 24.04.3, with cmake version 3.28.3. My nvcc is version 13.0.88 and my g++ is 13.3.0
.cuhon headers that should only be included from.cufiles (containing e.g. kernel/device function templates). Therefore it should bedevice.h.CMAKE_CUDA_COMPILERandCMAKE_CUDA_ARCHITECTURESis generally not a great idea. In most cases it makes sense to leave these up to the user who can set them via environment variables or argumentscmake -DCMAKE_CUDA_ARCHITECTURES=86. As long as your CTK is not in some unconventional location, you should not need to set it's path at all.