从常规 C++ 调用 CUDA 代码代码 - 整理出外部“C”
我试图从单独编译的 C++ 文件调用 CUDA(主机)函数:
sample.cpp
C++ 文件:
extern "C" void cuda_function(int a, int b);
int main(){
//statements
cuda_function(23, 34);
//statements
}
cuda.cu
文件:
#include <cuda.h>
__global__ void kernel(int a, int b)
{
//statements
}
void cuda_function(int a, int b){
//cuda_function
}
构建命令:
g++ -c sample.cpp
nvcc -c cuda.cu
nvcc -o sample sample.o cuda.o
但这给出了链接器错误:
sample.o: In function `main':
sample.cpp:(.text+0x163): undefined reference to `cuda_function'
collect2: ld returned 1 exit status
这种 C++ 和 CUDA 集成方法有什么问题?
I am trying to call a CUDA (host) function from a C++ file, compiled separately:
sample.cpp
C++ file:
extern "C" void cuda_function(int a, int b);
int main(){
//statements
cuda_function(23, 34);
//statements
}
cuda.cu
file:
#include <cuda.h>
__global__ void kernel(int a, int b)
{
//statements
}
void cuda_function(int a, int b){
//cuda_function
}
Build commands:
g++ -c sample.cpp
nvcc -c cuda.cu
nvcc -o sample sample.o cuda.o
But this gives linker error:
sample.o: In function `main':
sample.cpp:(.text+0x163): undefined reference to `cuda_function'
collect2: ld returned 1 exit status
What is wrong in this method of integration of C++ and CUDA?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将
cuda_function()
声明为extern "C"
,但随后使用 C++ 对其进行了定义。从你的声明中删除extern "C"
,它就会起作用。或者,您可以将相同的声明添加到 cuda.cu 文件中,但毫无意义。
详细来说,nvcc 是一个包装器,它将文件拆分为主机代码和设备代码,然后分别调用主机编译器和设备编译器。回到 CUDA 编程的旧时代,nvcc 以“C”模式调用主机编译器,这意味着从 C++ 调用时需要将
extern "C"
放在声明上。回到现在,nvcc 默认使用 C++ 作为主机代码,这意味着您不再需要这些外部程序(当然,除非主机代码的其余部分是 C 语言)。You have declared
cuda_function()
asextern "C"
, but then defined it using C++. Remove theextern "C"
from your delcaration and it will work.Alternatively, but pointlessly, you could add the same declaration to the cuda.cu file.
To elaborate a little, nvcc is a wrapper which splits a file into host code and device code and then calls the host compiler and device compiler respectively. Back in the old days of CUDA programming, nvcc called the host compiler in "C" mode wihch meant that you needed to put
extern "C"
on the delcarations when calling from C++. Returning to the present time, nvcc defaults to C++ for host code which means you shouldn't need those externs any more (unless the rest of your host code is in C of course).