Hi,
I encountered a problem trying to use CUDA code in the C project.
Here is a simple example to show this issue and I use cmake to organize different part.
There is even no device code, only the file suffix is .cu. And I find out that if the main file suffix is .cpp, it works.
I think the problem lies in the configuration items of the CMakeLists.txt, but I’m powerless to continue locating the problem.
top_dictionary
| main.c
| CMakeLists.txt
cuda_test
|_ cuda_test.cu
|_ cuda_test.h
|_ CMakeLists.txt
**top_dictionary:
//main.c:
include “cuda_test/cuda_test.h”
int main()
{
printHello(1);
}
//CMakeLists.txt:
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(test_src)
add_subdirectory(cuda_test)
include_directories(cuda_test)
set(test_source_file main.c)
add_executable(test_src ${test_source_file})
target_link_libraries(
test_src
cuda_test
)
**cuda_test dictionary:
//cuda_test.cu:
include <stdio.h>
include “cuda_test.h”
void printHello(int start_id)
{
printf("hello\n");
}
//cuda_test.h:
#ifndef CUDA_TEST_H_
define CUDA_TEST_H_
void printHello(int start_id);
//CMakeLists.txt:
cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
project (cuda_test)
find_package(CUDA REQUIRED)
option(CMAKE_VERBOSE_MAKEFILE ON)
option(DGTEST, “DGTEST” ON)
set(CUDA_NVCC_FLAGS -std=c++11)
set(CMAKE_CXX_STANDARD 11)
if (CUDA_VERBOSE_PTXAS)
set(VERBOSE_PTXAS --ptxas-options=-v)
endif (CUDA_VERBOSE_PTXAS)
set(GENCODE_SM86
-gencode=arch=compute_86,code=sm_86 -gencode=arch=compute_86,code=compute_86)
option(GPUBTREE_GENCODE_SM86 “GENCODE_SM86” ON)
if(GPUBTREE_GENCODE_SM86)
set(GENCODE ${GENCODE} ${GENCODE_SM86})
endif(GPUBTREE_GENCODE_SM86)
set(CUFILES
${CMAKE_CURRENT_SOURCE_DIR}/cuda_test.h
${CMAKE_CURRENT_SOURCE_DIR}/cuda_test.cu)
cuda_add_library(cuda_test STATIC
${CUFILES}
OPTIONS ${GENCODE} ${VERBOSE_PTXAS})
//Here is the error info:
[build] [2/3 66% :: 0.328] Linking CXX static library cuda_test/libcuda_test.a
[build] [3/3 100% :: 0.336] Linking C executable test_src
[build] FAILED: test_src
[build] : && /usr/bin/gcc -O3 -DNDEBUG CMakeFiles/test_src.dir/main.c.o -o test_src cuda_test/libcuda_test.a /usr/local/cuda-12.4/lib64/libcudart_static.a -ldl /usr/lib/x86_64-linux-gnu/librt.a && :
[build] /usr/bin/ld: CMakeFiles/test_src.dir/main.c.o: in function `main’:
[build] main.c:(.text.startup+0xe): undefined reference to `printHello’
[build] collect2: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[proc] The command: /usr/bin/cmake --build /home/mik/static_test/build --config Release --target all – exited with code: 1