I have a small c++ and cuda test project where I am trying to compile a simple setup containing a main.cpp file along with a test.cu and test.h file with some gpu functionality. My project setup looks like this:
project
│ CMakeLists.txt
│ main.cpp
| test.h
| test.cu
|
└───build
CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(test LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CUDA_STANDARD 14)
set(SOURCES ${SOURCES}
test.cu
)
add_executable(test main.cpp ${SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES CUDA_ARCHITECTURE "75")
test.h
#ifndef TEST_H_
#define TEST_H_
#include <cuda_runtime.h>
void test(int *arr, int size);
#endif // TEST_H_
test.cu
#include <cuda_runtime.h>
#include "test.h"
__global__ void test_kernel(int *arr, int size) {
arr[threadIdx.x] += 1;
}
void test(int *arr, int size) {
int *temp;
cudaMalloc(&temp, sizeof(int)*size);
cudaMemcpy(temp, arr, sizeof(int)*size, cudaMemcpyHostToDevice);
test_kernel<<<1, 256>>>(temp, size);
cudaMemcpy(arr, temp, sizeof(int)*size, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
cudaFree(temp);
}
main.cpp
#include <iostream>
#include "test.h"
int main(int argc, char **argv) {
int *arr = new int[10];
test(arr, 10);
delete[] arr;
return 0;
}
I am creating the build files and compiling by doing
cd build
cmake ..
make
however, when running make I get the following error
In file included from /home/bickit/Documents/test/main.cpp:3:
/home/bickit/Documents/test/test.h:4:10: fatal error: cuda_runtime.h: No such file or directory
4 | #include <cuda_runtime.h>
| ^~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/test.dir/build.make:76: CMakeFiles/test.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Why can I not compile my project like this when I have enabled CUDA as a language in my CMakeLists.txt?
My NVCC version is 11.7 and my cmake version is 3.24.1
You can definitely include your
test.hheader. The problem is thattest.hincludes, in turn,<cuda_runtime_api.h>; so the C++ compiler must have the directory containing this CUDA header - the CUDA distribution include directory - in its include path; and for that to happen, CMake must make sure and add that directory to the include path via the compiler's command-line.Note that for the NVCC compiler, the CUDA include directory is automatically searched without requiring specifying it on the command line; and NVCC is what's used by default to compile the
test.cufile - so you would not get the error message when includingtest.hintest.cu.To have CMake add the appropriate include directory when compiling C++, do this:
or directly add the relevant include directory with
target_include_directories()(not recommended).