这可能与Linker errors 2005 and 1169 (multiply defined symbols) when using CUDA __device__ functions (should be inline by default)类似,但不完全相同。尝试在VS2010上构建项目(使用已证明在其他地方可以使用的代码)时,出现几个LNK2005错误。我尽力了。
例如,我有以下三个文件:transposeGPU.h
,transposeGPU.cu
和transposeCUDA.cu
。 transposeGPU.h
可以总结如下:
void transposeGPU(float *d_dst, size_t dst_pitch,
float *d_src, size_t src_pitch,
unsigned int width, unsigned int height);
即一个不包含任何内容的声明。该功能的定义在
transposeGPU.cu
中,可以将其总结如下:#include <stdio.h>
#include "../transposeGPU.h"
#include "../helper_funcs.h"
#include "transposeCUDA.cu"
void
transposeGPU(float *d_dst, size_t dst_pitch,
float *d_src, size_t src_pitch,
unsigned int width, unsigned int height)
{
// execution configuration parameters
dim3 threads(16, 16);
dim3 grid(iDivUp(width, 16), iDivUp(height, 16));
size_t shared_mem_size =
(threads.x * threads.y + (threads.y - 1)) * sizeof(float);
transposeCUDA<<<grid, threads, shared_mem_size>>>(
d_dst, dst_pitch / sizeof(float),
d_src, src_pitch / sizeof(float),
width, height);
}
即
tranposeGPU.cu
包括其头文件和transposeCUDA.cu
,除了定义transposeGPU()
并调用transposeCUDA()
外,后者在transposeCUDA.cu
中找到。现在,transposeCUDA.cu
定义了预期的功能:#include "common_kernel.h"
__global__ void
transposeCUDA(
float *g_dst, size_t s_dst_pitch,
const float *g_src, size_t s_src_pitch,
unsigned int img_width, unsigned int img_height)
{
// several lines of code...
}
一切看起来都井然有序,但是我仍然在
error LNK2005: "void __cdecl __device_stub__Z13transposeCUDAPfjPKfjjj(float *,unsigned int,float const *,unsigned int,unsigned int,unsigned int)" (?__device_stub__Z13transposeCUDAPfjPKfjjj@@YAXPAMIPBMIII@Z) already defined in transposeCUDA.obj
中得到transposeGPU.obj
。那和其他二十多个类似的链接器错误。为什么?没有明显的重新定义。任何帮助将不胜感激。
最佳答案
如果同时编译transposeCUDA.cu和transposeGPU.cu,则会发生重新定义,因为定义同时出现在两个转换单元中。您不应该#include transposeCUDA.cu并将nvcc应用于该文件。
关于c++ - CUDA和链接器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5282681/