从常规 C++ 调用 CUDA 代码代码 - 整理出外部“C”

发布于 2024-09-30 19:56:07 字数 727 浏览 1 评论 0原文

我试图从单独编译的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-10-07 19:56:07

您已将 cuda_function() 声明为 extern "C",但随后使用 C++ 对其进行了定义。从你的声明中删除extern "C",它就会起作用。

或者,您可以将相同的声明添加到 cuda.cu 文件中,但毫无意义。

详细来说,nvcc 是一个包装器,它将文件拆分为主机代码和设备代码,然后分别调用主机编译器和设备编译器。回到 CUDA 编程的旧时代,nvcc 以“C”模式调用主机编译器,这意味着从 C++ 调用时需要将 extern "C" 放在声明上。回到现在,nvcc 默认使用 C++ 作为主机代码,这意味着您不再需要这些外部程序(当然,除非主机代码的其余部分是 C 语言)。

You have declared cuda_function() as extern "C", but then defined it using C++. Remove the extern "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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文