gpt4 book ai didi

c - C 和 CUDA-C 代码中的 NVCC 链接错误

转载 作者:行者123 更新时间:2023-11-30 17:50:44 24 4
gpt4 key购买 nike

我正在研究 DNA 片段组装项目。仅 CPU 版本是使用 GCC 用 C 语言构建的,我正在尝试使用 NVCC 构建 GPU 版本。

这是生成文件

all : clean FragmentAssembly.exe

FragmentAssembly.exe : Common.o Fragment.o ILS.o Consensus.o main.o
nvcc -pg -o FragmentAssembly.exe Common.o Fragment.o ILS.o Consensus.o main.o

Common.o : Common.cu
nvcc -pg -o Common.o -c Common.cu

Fragment.o : Fragment.cu
nvcc -pg -o Fragment.o -c Fragment.cu

ILS.o : ILS.cu
nvcc -pg -o ILS.o -c ILS.cu

Consensus.o : Consensus.cu
nvcc -pg -o Consensus.o -c Consensus.cu

main.o : main.cu
nvcc -pg -o main.o -c main.cu

clean :
rm -f *.exe *.o

正如所见,原始的.c 文件变成了.cu 文件,以便nvcc 正确编译它们。除了 main.cu 之外,所有 cu 文件都包含其相应文件(Common.h for Common.cu 等)。

ILS.h包含全局变量p_instanceFragmentsp_instanceLength的定义

问题是在编译 NVCC 时,由于未知原因,我收到以下错误:

Consensus.o:(.bss+0x0): multiple definition of `p_instanceFragments'
ILS.o:(.bss+0x0): first defined here
Consensus.o:(.bss+0x8): multiple definition of `p_instanceLength'
ILS.o:(.bss+0x8): first defined here

不存在真正的多重定义,因为相同的代码是使用 GCC 正确构建的。看起来,ILS.hILS.cuConsensus.cu 两次包含在 nvcc 中。这也是不可能的,因为我已经用 #ifndef .. #define .. #endif 语句包装了所有头文件,以避免多次包含和无限包含循环。

也许与 makefile 命令有关?或者我应该使用 gcc 进行链接?你能告诉我如何处理吗?

问候,

最佳答案

讨论后:这里是对所发生事件的描述:

如果您使用 gcc 并且有两个文件(假设 f1.cf2.c ),并且在这两个文件中声明:

int myGlobalVariable;

然后会发生以下情况:

  • 编译时f1.c进入f1.o ,目标文件将为 myGlobalVariable 保留空间.
  • 编译时f2.c进入f2.o ,目标文件还将为 myGlobalVariable 保留空间。 .
  • 当您将两个目标文件链接在一起时,链接器将检测到有两个名为 myGlobalVariable 的变量。并将这些变量合并在一起。

现在看来nvcc编译器/链接器无法能够合并这些变量。

问题是,文件 ILS.h声明<some type> p_instanceFragments; 。因为ILS.h均包含在 ILS.cu 中和Consensus.cu ,你会得到这个变量两次并且 nvcc当它必须链接应用程序时会提示。

解决方案是声明 extern <some type> p_instanceFragments;ILS.h然后定义<some type> p_instanceFragments;要么在 ILS.cu Consensus.cu (两者都不存在)。

问题What are extern variables in C有一个相当广泛的答案,详细解释了所有这些。

关于c - C 和 CUDA-C 代码中的 NVCC 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com