【问题标题】:Link error LNK2005 several CUDA files链接错误 LNK2005 几个 CUDA 文件
【发布时间】:2012-04-04 12:50:12
【问题描述】:

我遇到了错误“链接错误 LNK2005 ... 已定义”的一些问题。文件如下:

// 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

问题是,如果我没有在 Bitmap*.cu 之一中包含 #include "Bitmaps_cuda.h",当然,编译器会说我错过了定义的内核函数。我阅读了很多帖子,并且已经包含了“其他依赖项”和所需的路径。当我添加文件 Bitmap8.cu 及其相关内核时,问题就开始了,因为在此之前,应用程序工作正常。

无论如何,这些是我遇到的错误:

1>Bitmap8.cu.obj : error LNK2005: "void * __cdecl big_random_block(int(?big_random_block@@YAPAXH@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "int * __cdecl big_random_block_int(int(?big_random_block_int@@YAPAHH@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "unsigned char __cdecl value(float,float,int(?value@@YAEMMH@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void * __cdecl start_thread(unsigned int(__stdcall*)(void *),void *)" (?start_thread@@YAPAXP6GIPAX@Z0@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl end_thread(void *)"(?end_thread@@YAXPAX@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl destroy_thread(void *)"(?destroy_thread@@YAXPAX@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl wait_for_threads(void * const *,int)"(?wait_for_threads@@YAXPBQAXH@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl__device_stub__Z14float_to_colorPhPKf(unsigned char *,float const *)"(?__device_stub__Z14float_to_colorPhPKf@@YAXPAEPBM@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl float_to_color(unsigned char *,float_const *)" (?float_to_color@@YAXPAEPBM@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl__device_stub__Z14float_to_colorP6uchar4PKf(struct uchar4 *,float const *)"(?__device_stub__Z14float_to_colorP6uchar4PKf@@YAXPAUuchar4@@PBM@Z) already defined in Bitmap4.cu.obj
1>Bitmap8.cu.obj : error LNK2005: "void __cdecl float_to_color(struct uchar4 *,float_const *)" (?float_to_color@@YAXPAUuchar4@@PBM@Z) 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

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

提前谢谢你!

编辑

在网站 (http://msdn.microsoft.com/en-us/library/72zdcz6f.aspx) 上,我发现这些错误的原因之一是:
- 绝对值被定义了两次,每个定义中的值不同。
好吧,实际上,正如我在底部所写的那样,我有这些定义,但我不能做不同的事情。知道如何解决吗?

再次感谢您

【问题讨论】:

  • 我包含了两次头文件!问题解决了!

标签: c++ linker cuda


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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 文件使用的运行时相同.

    【讨论】:

    • 在头文件中包含内核定义是完全合法的,只要该头文件只被导入一次,并且通过设备代码编译轨迹。像 Thrust 这样的模板库广泛地做到了这一点。
    • "在头文件中包含内核定义是完全合法的,只要该头文件只被导入一次,并且通过设备代码编译轨迹。"是的,但是你真的有一个头文件吗?这更像是您有一个伪装成头文件的实现文件,这将导致无穷无尽的混乱。同意的,模板化内核定义是一个例外。
    • 好吧,Roger Dahl 的解决方案帮助我解决了其他问题,但编译器仍然给我与我上面发布的相同的错误!我无法理解,因为当我添加更多内核定义和内核函数时问题就开始了。老实说,除了内部的一些参数外,部分内核函数正在执行相同的操作,但是我仍然无法编译,这很奇怪。
    • @DavideBerdin:如果你能让你的整个项目在某个地方可用,我会看看它。
    • @DavideBerdin:我不明白你为什么接受了一个答案,然后又打开了另一个几乎相同的问题,而这显然没有解决你的问题????
    猜你喜欢
    • 2010-12-30
    • 2016-10-05
    • 1970-01-01
    • 2011-10-01
    • 2010-11-26
    • 1970-01-01
    • 2022-01-13
    • 2013-04-30
    • 2011-07-14
    相关资源
    最近更新 更多