Cuda C - Linker错误-未定义的引用。

时间:2022-11-25 05:37:49

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

我很难编译一个由两个文件组成的简单cuda程序。

The main.c looks like this:

主要的。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.

谢谢你的帮助。

2 个解决方案

#1


18  

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。比如:

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进行链接,在这种情况下,整个问题应该会消失。

#2


5  

You have a C/C++ linkage problem. nvcc is decorating things in a C++ fashion but your gcc compiler is handling things using C style linkage. 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

你有一个C/ c++联动问题。nvcc正在以c++的方式装饰东西,但是您的gcc编译器正在使用C风格的链接来处理东西。修复它的一个简单方法是重命名main。c主要。然后使用g++而不是gcc来重复您的命令

#1


18  

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。比如:

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进行链接,在这种情况下,整个问题应该会消失。

#2


5  

You have a C/C++ linkage problem. nvcc is decorating things in a C++ fashion but your gcc compiler is handling things using C style linkage. 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

你有一个C/ c++联动问题。nvcc正在以c++的方式装饰东西,但是您的gcc编译器正在使用C风格的链接来处理东西。修复它的一个简单方法是重命名main。c主要。然后使用g++而不是gcc来重复您的命令