c++ - 几个CUDA文件链接错误LNK2005

标签 c++ linker cuda

我遇到了错误“链接错误 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++ - 几个CUDA文件链接错误LNK2005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10011460/

相关文章:

c++ - Qt删除QTableView中选中的行

c++ - `GamepadControll::GamepadControll()'的多重定义

python - 在 PyCUDA 中遍历二维数组

c++ - 引导加载程序上的线程

c++ - 功能模板的部分属性在 GCC 中被静默忽略

C++ 静态唯一方法?

c++ - 为什么我突然从游戏项目中获得引用我的游戏引擎的未解析外部符号

c++ - 如何在可执行共享库 (.so) 中触发全局变量的 c'tors?

(int) -1 的 CUDA 内核 printf 给出带有 %d 说明符的错误输出

cudaThreadSynchronize() 要求