CUDA C - 链接错误 - 未定义参考 [英] Cuda C - Linker error - undefined reference
问题描述
我有一个很难编译只有两个文件,一个简单的CUDA程序consiting。
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文件洛斯是这样的:
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 ++编译器编译主机code,这意味着,符号的名称重整是应用于由CUDA工具链发出code。
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.
有两个解决这个问题。首先是定义 dummy_gpu
使用C键,这样修改你的 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屋!