2012-04-04 107 views
1

我得到了一些问题,“链接错误LNK2005 ...已经定义”的错误。该文件如下:链接错误LNK2005几个CUDA文件

// File Bitmap4.cu 
#include "Bitmap4.h" // header 
#include "Bitmaps_cuda.h" // header with just the definitions of the kernels 

..... // I call 3+2 kernel functions (3 in one method, 1 in another and 1 in another one) 

然后,我有这样一条:

// File Bitmap8.cu 
#include "Bitmap8.h" // header 
#include "Bitmaps_cuda.h" // the same as above 

..... // I call 4 kernel functions (4 in the same method) 

然后,我有内核头:

#ifndef __BITMAPS_KERNEL__ 
#define __BITMAPS_KERNEL__ 

...... // 9 kernels definitions 

#endif 

最后,我有这样的一个:

// File Bitmaps_cuda.h 
#include <cuda.h> 
#include <cuda_runtime.h> 
#include <device_launch_parameters.h> 
#include <device_functions.h> 
#include <stdio.h> 

// Inside here there all the kernel functions that the files 
// Bitmap4.cu and Bitmap8.cu are using 

问题是,如果我不' t包括#include“Bitmaps_cuda.h”在Bitmap * .cu中的一个,当然,编译器会说我错过了内核函数的定义。我读了很多帖子,并且我已经包含了“附加依赖关系”和所需的PATH。当我将文件Bitmap8.cu与相关的内核相加时,问题就开始了,因为在此之前,应用程序工作正常。

无论如何,这些都是我有错误:

1>Bitmap8.cu.obj : error LNK2005: "void * __cdecl big_random_block(int([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "int * __cdecl big_random_block_int(int([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "unsigned char __cdecl value(float,float,int([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void * __cdecl start_thread(unsigned int(__stdcall*)(void *),void *)" ([email protected]@[email protected]@Z) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl end_thread(void *)"([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl destroy_thread(void *)"([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl wait_for_threads(void * const *,int)"([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl__device_stub__Z14float_to_colorPhPKf(unsigned char *,float const *)"([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl float_to_color(unsigned char *,float_const *)" ([email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl__device_stub__Z14float_to_colorP6uchar4PKf(struct uchar4 *,float const *)"([email protected]@[email protected]@[email protected]) already defined in Bitmap4.cu.obj 
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl float_to_color(struct uchar4 *,float_const *)" ([email protected]@[email protected]@[email protected]) already defined in Bitmap4.cu.obj 

1>C:\Users\dberdin\documents\visual studio 2010\Projects\gpuSPAM\Debug\gpuSPAM.exe : fatal error LNK1169: one or more multiply defined symbols found 

我尝试了不同的解决方案,但与任何结果。

预先感谢您!

EDIT

在网站上(http://msdn.microsoft.com/en-us/library/72zdcz6f.aspx)我发现,这些错误的原因之一是:
- 绝对被定义了两次,在每个定义不同的值。
嗯,实际上,正如我在底部写的那样,我有这样的定义,但我无法改变。任何想法如何解决它?

再次感谢您提前

+0

我包含一个头文件两次!问题解决了! – davideberdin 2012-04-05 14:31:20

回答

2

这些错误是因为我包含一个文件。问题解决了!

1
Then I have the kernel header: 

#ifndef __BITMAPS_KERNEL__ 
#define __BITMAPS_KERNEL__ 

...... // 9 kernels definitions 

#endif 

您的意思是说,你有9页内核的声明,而不是定义?

您不能在头文件中拥有内核定义。

确保所有的.CU文件链接到同一运行(打开每个文件.CU属性表和比较CUDA C/C++ | Host | Runtime Library设置。)另外,还要确保是所使用的常规cpp文件相同运行。

+0

只要头文件只导入一次,并且通过设备代码编译轨迹,在头文件中定义内核定义是完全合法的。像Thrust这样的模板库可以做到这一点。 – talonmies 2012-04-04 16:34:17

+0

“只要头文件只导入一次,并且通过设备代码编译轨迹,在头文件中定义内核是完全合法的。”诚然,但你真的有一个头文件吗?这更像是你有一个伪装成头文件的实现文件,这将不会导致混淆。同意的模板化内核定义将是一个例外。 – 2012-04-04 17:10:57

+0

好吧,Roger Dahl的解决方案帮助我解决了其他问题,但编译器仍然给我提供与上面一样的错误!我无法得到它,因为我添加了更多的内核定义和内核函数时就开始出现问题。说实话,内核函数的一部分除了内部的一些参数外,还在做相同的操作,但是仍然很奇怪我不能编译。 – davideberdin 2012-04-05 11:36:12