1

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:

  1. main.cpp
  2. host.cpp
  3. host.h
  4. device.cu
  5. device.cuh
  6. 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

7
  • which version of cuda? how did you install it? Which linux distro? Commented Nov 4 at 21:12
  • @AlanBirtles your'e right, my bad for not including this data. I now modified the question to included these Commented Nov 4 at 21:24
  • CUDA 13 is quite new and a major release, i.e. it includes breaking changes that older CMake versions are not adapted to. Try to get the most recent version of CMake. Commented Nov 4 at 23:44
  • Convention is to put .cuh on headers that should only be included from .cu files (containing e.g. kernel/device function templates). Therefore it should be device.h. Commented Nov 4 at 23:52
  • Hardcoding CMAKE_CUDA_COMPILER and CMAKE_CUDA_ARCHITECTURES is 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 arguments cmake -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. Commented Nov 4 at 23:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.