简体   繁体   English

将错误与CUDA和GCC链接

[英]Linking errors with CUDA and GCC

I've been trying to compile a set of C and CUDA code. 我一直在尝试编译一组C和CUDA代码。 The problem lies within the linking stage of the compilation when I make the file. 问题出在我制作文件时处于编译的链接阶段。 I made a wrapper function to be executed on the host to allocate memory on the device, copy data to it, and run the kernel code. 我做了一个包装函数,要在主机上执行以分配设备上的内存,将数据复制到该设备上,然后运行内核代码。 Also, the wrapper code is contained within the same file as the kernel code. 此外,包装程序代码与内核代码包含在同一文件中。 The wrapper . 包装器。 Here is how the code calls the function: 代码是如何调用函数的:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "LAMMPS.h"

...

main(int argc, char **argv)
{
    ...
tortuosityTotal = gradient(nbAtoms, topAtoms, bottomAtoms, nTop, nBottom, allConnections, atomlist, ysize);

Here is the function definition in the header file "LAMMPS.h" which I made: 这是我制作的头文件“ LAMMPS.h”中的函数定义:

float gradient(unsigned short nbAtoms, unsigned short *topAtoms,unsigned short
              *bottomAtoms, unsigned short nTop, unsigned short nBottom, struct connection
              **allConnections,struct atoms *atomlist, double *ysize);

And here is the makefile I am using: 这是我正在使用的makefile:

all: tortGPU

tortGPU: gradient_kernel.o buildNeighborList.o dumpRead.o tortuosityGPU.c
    nvcc tortuosityGPU.c buildNeighborList.o dumpRead.o gradient_kernel.o -lm -o tortGPU
buildNeighborList.o: dumpRead.o buildNeighborList.c
    gcc -c buildNeighborList.c
dumpRead.o: dumpRead.c
    gcc -c dumpRead.c
gradient_kernel.o:
    nvcc -c gradient_kernel.cu -arch=sm_20
clean: rm -rf *.o program

Finally, the compilation steps work fine, however when I go to link them all together (the final step with tortGPU I get the following error message: 最后,编译步骤工作正常,但是当我将它们链接在一起时(使用tortGPU的最后一步,我得到以下错误消息:

/tmp/tmpxft_000068c7_00000000-1_tortuosityGPU.o: In function `main':
tortuosityGPU.c:(.text+0x520): undefined reference to `gradient'
collect2: ld returned 1 exit status
make: *** [tortGPU] Error 1

I tried using gcc with the addition -L and gotten the exact same error code. 我尝试将gcc与-L一起使用,并获得了完全相同的错误代码。 Thanks! 谢谢!

Although the language is often called cuda-C , it is more correct to call it cuda-C++ as nvcc is a C++ compiler. 尽管该语言通常被称为cuda-C ,但将其称为cuda-C++更正确,因为nvcc是C ++编译器。 The function named gradient is being subjected to C++ name mangling (or, more accurately, nvcc is looking for a mangled name). 正在对名为gradient的函数进行C ++名称修饰(或更准确地说, nvcc正在查找修饰的名称)。 You could either wrap your declarations with extern "C" or compile your code as C++. 您可以使用extern "C"包装声明,也可以将代码编译为C ++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM