Cuda C - 链接器错误 - 未定义的引用 [英] Cuda C - Linker error - undefined reference

查看:809
本文介绍了Cuda C - 链接器错误 - 未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难编译一个只有两个文件的简单cuda程序。

I am having a hard time compiling a simple cuda program consiting of only two files.

main.c看起来像这样:

The main.c looks like this:

#include "my_cuda.h"

int main(int argc, char** argv){
   dummy_gpu();
}

cuda.h看起来像这样:

The cuda.h looks like this:

#ifndef MY_DUMMY
#define MY_DUMMY

void dummy_gpu();

#endif

并且my_cuda.cu文件如下: / p>

And the my_cuda.cu file loos like this:

#include <cuda_runtime.h>
#include "my_cuda.h"

__global__ void dummy_gpu_kernel(){
   //do something
}

void dummy_gpu(){
   dummy_gpu_kernel<<<128,128>>>();
}

然而,如果我编译我总是收到以下错误:

However if I compile I allways receive the following error:

gcc  -I/usr/local/cuda/5.0.35/include/ -c main.c
nvcc  -c my_cuda.cu
gcc  -L/usr/local_rwth/sw/cuda/5.0.35/lib64 -lcuda -lcudart -o md.exe main.o my_cuda.o 
main.o: In function `main':
main.c:(.text+0x15): undefined reference to `dummy_gpu'
collect2: ld returned 1 exit status

感谢您的帮助。

推荐答案

nvcc 使用主机C ++编译器来编译主机代码,这意味着符号 name mangling 应用于CUDA工具链发出的代码。

You have a problem with symbol name mangling. nvcc uses the host C++ compiler to compile host code, and this implies that symbol name mangling is applied to code emitted by the CUDA toolchain.

这个问题有两种解决方案。第一个是使用C链接定义 dummy_gpu ,所以改变你的 my_cuda.cu 到这样:

There are two solutions to this problem. The first is to define dummy_gpu using C linkage, so change your my_cuda.cu to something like this:

extern "C" {
#include "my_cuda.h"
}

.....


extern "C"
void dummy_gpu(){
   dummy_gpu_kernel<<<128,128>>>();
}

请注意,您需要将连接命令更改为:

Note that you will need to change your linkage command to this:

gcc -L/usr/local_rwth/sw/cuda/5.0.35/lib64 -o md.exe main.o my_cuda.o -lcuda -lcudart 

,因为CUDA共享库需要在

because the CUDA shared libraries need to be specified after the object files that use them.

您的第二个选择是使用 g ++ nvcc 进行链接,在这种情况下,整个问题应该消失。

Your second alternative would be to use either g++ or nvcc to do the linking, in which case the whole problem should disappear.

这篇关于Cuda C - 链接器错误 - 未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆