尝试一起编译多个CUDA文件时,链接错误LNK2005 [英] Link error LNK2005 when trying to compile several CUDA files together

查看:278
本文介绍了尝试一起编译多个CUDA文件时,链接错误LNK2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作正常的CUDA程序,但这是目前都写在一个文件。

I have a CUDA program that works fine, but that is currently all written in one file. I'd like to split this big file into several smaller ones, in order to make it easier to maintain and navigate.

新的结构是:

foo.cuh
foo.cu
bar.cuh
bar.cu
main.cu

.cuh 头文件包含结构函数原型, .cu 文件包含函数定义(像往常一样)。主文件包括 bar.cuh bar.cu 包括 foo.cuh 。所有.cu文件包括cutil_inline.h,以便能够使用CUDA函数。

The .cuh header files contain structs and function prototypes, and the .cu files contain the function definitions (as usual). The main file includes bar.cuh, and bar.cu includes foo.cuh. All the .cu files include cutil_inline.h, in order to be able to use the CUDA functions.

因此:

// main.cu
#include "bar.cuh"
#include <cutil_inline.h>

int main() [...]

// bar.cu
#include "bar.cuh"
#include "foo.cuh"
#include <cutil_inline.h>

[...]

// foo.cu
#include "foo.cuh"
#include <cutil_inline.h>

[...]

问题是, Visual Studio 2008项目这个新的结构,我得到吨链接错误:

The problem is that when I compile my Visual Studio 2008 project with this new structure, I get tons of link errors :

error LNK2005: "void __cdecl __cutilBankChecker(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,char *,int,char *,int)" (?__cutilBankChecker@@YAXIIIIIIPADH0H@Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cutilCondition(int,char *,int)" (?__cutilCondition@@YAXHPADH@Z) already defined in cuda_generated_foo.cu.obj cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cutilExit(int,char * *)" (?__cutilExit@@YAXHPAPAD@Z) already defined in cuda_generated_foo.cu.obj    cuda_generated_bar.cu.obj
error LNK2005: "int __cdecl cutGetMaxGflopsDeviceId(void)" (?cutGetMaxGflopsDeviceId@@YAHXZ) already defined in cuda_generated_foo.cu.obj   cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeCallNoSync(enum cudaError,char const *,int)" (?__cudaSafeCallNoSync@@YAXW4cudaError@@PBDH@Z) already defined in cuda_generated_foo.cu.obj    cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeCall(enum cudaError,char const *,int)" (?__cudaSafeCall@@YAXW4cudaError@@PBDH@Z) already defined in cuda_generated_foo.cu.obj    cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cudaSafeThreadSync(char const *,int)" (?__cudaSafeThreadSync@@YAXPBDH@Z) already defined in cuda_generated_foo.cu.obj    cuda_generated_bar.cu.obj
error LNK2005: "void __cdecl __cufftSafeCall(enum cufftResult_t,char const *,int)" (?__cufftSafeCall@@YAXW4cufftResult_t@@PBDH@Z) already defined in cuda_generated_foo.cu.obj  cuda_generated_bar.cu.obj



我理解它们的意思(所有已经定义的符号都是cutil_inline.h的一部分),但我必须在所有文件中包含此标题,否则不会编译。我做错了什么?

I understand what they mean (all those symbols already defined are part of cutil_inline.h) but I have to include this header in all files, otherwise it does not compile. What am I doing wrong ?

更新:为了澄清情况:
*与一个大文件中的所有代码,它编译,链接和运行罚款
*与新的结构(几个较小的文件),并包括所有.cu文件中的cutil_inline.h,它编译正确,但失败在链接
*与新的结构,包括cutil_inline.h只在主文件,它在编译期间失败,表示cutil函数在未包含cutil_inline.h的文件中未知(如预期,但我必须尝试一切)
- 列表项

UPDATE: To clarify the situation : * with all code in one big file, it compiles, links and runs fine * with the new structure (several smaller files) and including cutil_inline.h in all .cu files, it compiles correctly but fails during linking * with the new structure and including cutil_inline.h only in the main file, it fails during compilation, saying that the cutil function are unknown in the files where cutil_inline.h was not included (as expected, but I had to try everything) - List item

推荐答案

这个错误也发生在我的程序。我通过在 __全局__ __设备__ inline >。然后,错误消失了。

This error also happened in my program. I solved it by adding the keyword inline before __global__ or __device__. Then, the error went away.

这篇关于尝试一起编译多个CUDA文件时,链接错误LNK2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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