2012-04-04 54 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