[英]Cuda C - Linker error - undefined reference
I am having a hard time compiling a simple cuda program consiting of only two files.我很难编译一个只包含两个文件的简单 cuda 程序。
The main.c looks like this: main.c 看起来像这样:
#include "my_cuda.h"
int main(int argc, char** argv){
dummy_gpu();
}
The cuda.h looks like this: cuda.h 看起来像这样:
#ifndef MY_DUMMY
#define MY_DUMMY
void dummy_gpu();
#endif
And the my_cuda.cu file loos like this: my_cuda.cu 文件看起来像这样:
#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
Thank you for your help.感谢您的帮助。
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. nvcc
使用主机 C++ 编译器来编译主机代码,这意味着符号名称修改应用于 CUDA 工具链发出的代码。
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:第一个是使用 C 链接定义
dummy_gpu
,因此将my_cuda.cu
更改为如下所示:
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
because the CUDA shared libraries need to be specified after the object files that use them.因为需要在使用它们的目标文件之后指定 CUDA 共享库。
Your second alternative would be to use either g++
or nvcc
to do the linking, in which case the whole problem should disappear.您的第二个选择是使用
g++
或nvcc
进行链接,在这种情况下,整个问题应该消失。
You have a C/C++ linkage problem.您有 C/C++ 链接问题。 nvcc is decorating things in a C++ fashion but your gcc compiler is handling things using C style linkage.
nvcc 以 C++ 方式装饰事物,但您的 gcc 编译器使用 C 风格链接处理事物。 A simple way to fix it is to rename your main.c to main.cpp and then repeat your commands using g++ instead of gcc
修复它的一个简单方法是将您的 main.c 重命名为 main.cpp,然后使用 g++ 而不是 gcc 重复您的命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.