【问题标题】:Cuda error: function has already been defined in another .cu.obj fileCuda 错误:函数已在另一个 .cu.obj 文件中定义
【发布时间】:2011-07-11 01:31:02
【问题描述】:

我正在尝试编译某人发给我的 cuda 项目。尽管编译阶段通过了,但链接阶段却失败了。以下是错误示例:

Error   298 error LNK2005: "int __cdecl compare_ints(void const *,void const *)" (?compare_ints@@YAHPBX0@Z) already defined in 3level_1.cu.obj  decode_p4.cu.obj    

基本上,文件 decode_p4.cu.obj 抱怨函数 compare_ints 已经在 3level_1.cu.obj 中定义。关于如何避免这种行为的任何想法?

如果有帮助,下面是类似错误的列表:

Error   384 error LNK2005: "int __cdecl compare_ints(void const *,void const *)" (?compare_ints@@YAHPBX0@Z) already defined in 3level_1.cu.obj  decode_p4.cu.obj    god
Error   385 error LNK2005: "int __cdecl cpu_intersection(unsigned int *,int,unsigned int *,int)" (?cpu_intersection@@YAHPAIH0H@Z) already defined in 3level_1.cu.obj    decode_p4.cu.obj    god
Error   386 error LNK2005: "int __cdecl intersection_cpu(unsigned int * * const,int * const,int)" (?intersection_cpu@@YAHQAPAIQAHH@Z) already defined in 3level_1.cu.obj    decode_p4.cu.obj    god
Error   387 error LNK2005: "void __cdecl sort_it(unsigned int * * const,int * const,int)" (?sort_it@@YAXQAPAIQAHH@Z) already defined in 3level_1.cu.obj decode_p4.cu.obj    god
Error   388 error LNK2005: "int __cdecl GPU_Intersection(unsigned int * * const,int * const,int,unsigned int *,unsigned int *,unsigned int *,struct uint4 *)" (?GPU_Intersection@@YAHQAPAIQAHHPAI22PAUuint4@@@Z) already defined in 3level_1.cu.obj decode_p4.cu.obj    god
Error   389 error LNK2005: "int __cdecl ceilPow2(int)" (?ceilPow2@@YAHH@Z) already defined in 3level_1.cu.obj   decode_p4.cu.obj    god
Error   390 error LNK2005: "void __cdecl recAllocate1(int,int)" (?recAllocate1@@YAXHH@Z) already defined in 3level_1.cu.obj decode_p4.cu.obj    god
Error   391 error LNK2005: "unsigned int __cdecl getceilPow2(unsigned int)" (?getceilPow2@@YAII@Z) already defined in 3level_1.cu.obj   decode_p4.cu.obj    god
Error   392 error LNK2005: "void __cdecl runTest(int,char * *)" (?runTest@@YAXHPAPAD@Z) already defined in 3level_1.cu.obj  decode_p4.cu.obj    god
Error   393 error LNK2005: "void __cdecl __device_stub__Z13scanBlockMAX1P5uint4S0_Pj(struct uint4 *,struct uint4 *,unsigned int *)" (?__device_stub__Z13scanBlockMAX1P5uint4S0_Pj@@YAXPAUuint4@@0PAI@Z) already defined in 3level_1.cu.obj  decode_p4.cu.obj    god
Error   394 error LNK2005: "void __cdecl scanBlockMAX1(struct uint4 *,struct uint4 *,unsigned int *)" (?scanBlockMAX1@@YAXPAUuint4@@0PAI@Z) already defined in 3level_1.cu.obj  decode_p4.cu.obj    god
Error   395 error LNK2005: "void __cdecl __device_stub__Z16scanBlockMAX1_gpP5uint4S0_Pj(struct uint4 *,struct uint4 *,unsigned int *)" (?__device_stub__Z16scanBlockMAX1_gpP5uint4S0_Pj@@YAXPAUuint4@@0PAI@Z) already defined in 3level_1.cu.obj    decode_p4.cu.obj    god

【问题讨论】:

  • 各位,请帮帮我。我必须尽快这样做

标签: cuda gpgpu nvidia gpu


【解决方案1】:

这只是一个猜测,但如果这是 Visual Studio,我以前见过这种情况,当时代码位于 #included 的 .cu 文件中。在这种情况下,它也不应该被编译为源文件。为避免这种情况,请右键单击其中一个文件(很难从您的描述中分辨出哪个文件)并选择属性,然后找到并选中“从构建中排除”复选框。

【讨论】:

  • 谢谢,当我从构建中排除(包含)文件时,这对我有用。
【解决方案2】:

看起来你的 decode_p4.cu 3level_1.cu 包含多个这些函数,重点是定义这些函数的文件。

确保将声明放入 .cuh 文件(与 c 中的头文件相同)并包含 cuh 而不是 cu 文件,然后查看 include guards

祝你好运,兄弟!

【讨论】:

    【解决方案3】:

    对多个文件有相同的问题,并且因为多个定义问题通常由守卫ala来处理而感到困惑。

    # ifndef Function_name_Guard
    #define Function_name
    your code
    #endef % Function_name_Guard
    

    有效的方法是省略 .cu 文件,只保留主文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多