【问题标题】:Linker error 2019 while linking Winusb application and CUDA链接 Winusb 应用程序和 CUDA 时出现链接器错误 2019
【发布时间】:2014-11-18 16:19:22
【问题描述】:

我有两个应用程序 Winusb 并使用 CUDA 将数据从 cpu 处理到 GPU。两者都可以作为两个单独的项目正常工作,但是当我尝试将它们组合为一个项目时,出现以下链接错误。

构建日志:

1>------ Build started: Project: USB Application2, Configuration: Win7 Debug Win32 ------
1>  Compiling CUDA source file function.cu...
1>  
1>  C:\Users\bel1\documents\visual studio 2012\Projects\USB Application2\USB Application2>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=sm_10 --use-local-env --cl-version 2010 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -IWin7Debug\ -IWin7Debug\ -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"     --keep-dir Win7Debug -maxrregcount=0  --machine 32 -cuda -cudart static     -D_X86_=1 -Di386=1 -DSTD_CALL -DWIN32_LEAN_AND_MEAN=1 -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DWINNT=1 -DNTDDI_VERSION=0x06010000 -DDBG=1 -D_AFXDLL -Xcompiler "/EHsc /W0 /nologo /Od /Zi /RTC1 /MT  " -o Win7Debug\function.cu.obj "C:\Users\bel1\documents\visual studio 2012\Projects\USB Application2\USB Application2\function.cu" 
1>  Compiling CUDA source file wrapper.cu...
1>  
1>  C:\Users\bel1\documents\visual studio 2012\Projects\USB Application2\USB Application2>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=sm_10 --use-local-env --cl-version 2010 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -IWin7Debug\ -IWin7Debug\ -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"     --keep-dir Win7Debug -maxrregcount=0  --machine 32 -cuda -cudart static     -D_X86_=1 -Di386=1 -DSTD_CALL -DWIN32_LEAN_AND_MEAN=1 -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DWINNT=1 -DNTDDI_VERSION=0x06010000 -DDBG=1 -D_AFXDLL -Xcompiler "/EHsc /W0 /nologo /Od /Zi /RTC1 /MT  " -o Win7Debug\wrapper.cu.obj "C:\Users\bel1\documents\visual studio 2012\Projects\USB Application2\USB Application2\wrapper.cu" 
1>main.obj : error LNK2019: unresolved external symbol "short * __stdcall speccud(short * const)" (?speccud@@YGPAFQAF@Z) referenced in function _main
1>C:\Users\bel1\documents\visual studio 2012\Projects\USB     Application2\Win7Debug\USBApplication2.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Main.cpp

 #include "pch.h"
 short *speccud(short ispecbu[]);

 LONG __cdecl _tmain(LONG  Argc, LPTSTR ** Argv )
 //int main(int argc, char **argv)
 {  
FILE *output_file1 = fopen("output_file2.txt", "w");
FILE *output_file2 = fopen("output_file3.txt", "w");
DEVICE_DATA           deviceData;
HRESULT               hr;
USB_DEVICE_DESCRIPTOR deviceDesc;
BOOL                  bResult;
BOOL                  noDevice;
ULONG                 lengthReceived;
BOOL wrResult = TRUE;
BOOL wr1Result = TRUE;
BOOL RQResult = 0;
UNREFERENCED_PARAMETER(Argc);
UNREFERENCED_PARAMETER(Argv);


//////////////////////Open device ///////////////
hr = OpenDevice(&deviceData, &noDevice);
if (FAILED(hr)) {
    if (noDevice) {
        printf(_T("Device not connected or driver not installed\n"));
    } else {
       printf(_T("Failed looking for device, HRESULT 0x%x\n"), hr);
    }
    std::getchar();
    return 0;
}

/////////////////////Get descriptor//////////////////
bResult = WinUsb_GetDescriptor(deviceData.WinusbHandle,
                               USB_DEVICE_DESCRIPTOR_TYPE,
                               0,
                               0,
                               (PBYTE) &deviceDesc,
                               sizeof(deviceDesc),
                               &lengthReceived);
   if (FALSE == bResult || lengthReceived != sizeof(deviceDesc)) {

    printf(_T("Error among LastError %d or lengthReceived %d\n"),
           FALSE == bResult ? GetLastError() : 0,
           lengthReceived);
    CloseDevice(&deviceData);
    return 0;
}

bool sResult = FALSE;bool syResult;
bool sResult1 = FALSE;bool syResult1;
//Initialize
UCHAR Intialize[] = {0x01};
ULONG cbISize = strlen((char*)Intialize);
ULONG InSent = 0;
wrResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Intialize, 1, &InSent, 0);
//Integration time - 700ms
UCHAR Inttime[] = {0x0200100000};
ULONG cbITSize = strlen((char*)Inttime);
ULONG InttimeSent = 0;
wrResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Inttime, 5, &InttimeSent, 0);
//strobe signal
UCHAR strobe1[] = {0x030001};
ULONG strobeSize1 = strlen((char*)strobe1);
ULONG strobeSent1 = 0;
wr1Result = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, strobe1, 3, &strobeSent1, 0);
//Request spectra
UCHAR Rqspectra[] = {0x09};
ULONG RqSize = strlen((char*)Rqspectra);
ULONG RqSent = 0;
RQResult = WinUsb_WritePipe(deviceData.WinusbHandle, 0x01, Rqspectra,1, &RqSent, 0);
//Pixel Values
UCHAR szBuffer[15][512];
UCHAR sz1Buffer[1];
UCHAR tBuffer[1];
ULONG tReadx;
ULONG cbReadx[16];
USHORT newbuf[15][512];
short specbu[7860];
short *fans;
for (int i=0;i<16;i++)
{
    if (i<4)
    {
        sResult = WinUsb_ReadPipe(deviceData.WinusbHandle,  0x86, szBuffer[i], 512, &cbReadx[i], 0);
    }
    else if (i>=4 && i<15)
    {
        sResult = WinUsb_ReadPipe(deviceData.WinusbHandle,  0x82, szBuffer[i], 512, &cbReadx[i], 0);
    }
    else if (i = 15)
    {
        syResult = WinUsb_ReadPipe(deviceData.WinusbHandle,  0x82, sz1Buffer, 1, &cbReadx[i], 0);
    }

}
int pon=0;
for (int k=0;k<15;k++)
{
for (int l=0;l<512;l+=2)
{
    newbuf[k][l] = (szBuffer[k][(l+1)]<<8|szBuffer[k][l]);
    specbu[pon]= (szBuffer[k][(l+1)]<<8|szBuffer[k][l]);
    fprintf(output_file1,"%d\t\n",specbu[pon]);
    pon++;

}
}
//printf("%d",sizeof(specbu));
//short ARR[5] = {1,2,3,4,5}; 
//fans=speccud(ARR);
fans = speccud(specbu);
for (int k=0;k<5;k++)
{printf("%d", fans[k]);
    //fprintf(output_file2,"%d\t\n",fans[k]);
    //tempc[k]=specbu[k];
}
std::getchar();
CloseDevice(&deviceData);
return 0;
}

包装器.cu

#include "cuda_runtime.h"
#include <cuda.h>
#include <cstdio>
#include "device_launch_parameters.h"


__global__ void saxpy(int cn, short ca, short *cx, short *cy);
short *speccud(short ispecbu[])
{
  const int cN = 7680;
  short *cx,  *cd_x, *cd_y;
  static short *cy;
  cx = (short*)malloc(cN*sizeof(short));
  cy = (short*)malloc(cN*sizeof(short));
  cudaMalloc(&cd_x, cN*sizeof(short)); 
  cudaMalloc(&cd_y, cN*sizeof(short));
 for (int ci = 0; ci < cN; ci++) {
  cx[ci] = ispecbu[ci];
  //y[i] = 2.0f;
  }
 cudaMemcpy(cd_x, cx, cN*sizeof(short), cudaMemcpyHostToDevice);
 cudaMemcpy(cd_y, cy, cN*sizeof(short), cudaMemcpyHostToDevice);
 // Perform SAXPY on 1M elements
 saxpy<<<(cN+255)/256, 256>>>(cN, 1, cd_x, cd_y);
 cudaMemcpy(cy, cd_y, cN*sizeof(short), cudaMemcpyDeviceToHost);
 return cy;
 }

函数.cu

#ifndef __Kernel_CU__  
#define __Kernel_CU__  
 __global__ void saxpy(int cn, short ca, short *cx, short *cy)
{
 int ci = blockIdx.x*blockDim.x + threadIdx.x;
  cy[ci] = ca+cx[ci];
}

#endif

我还有一个device.cpp,它有opendevice、closedevice等功能(Winusb的一部分,自动生成的代码行太多)。它看起来像是 Cuda 和 Winusb 应用程序之间的某种链接器错误。当我简单地为 Cuda 和另一个为 winusb 创建两个 Visual Studio 项目时,两者都可以完美运行。

【问题讨论】:

  • 这里的问题是链接器找不到speccud函数。您确定您的项目已设置为编译和构建您的 CU 文件吗?我假设不是,因为那是该函数的实现所在。
  • @Preston 我更新了构建日志。你可以看到它说编译 function.cu 和 wrapper.cu。我什至删除了以前的 .cu 文件并再次创建并链接它,但最终还是出现同样的错误。我错过了什么吗?
  • 您的 CU 文件中的名称是否可能被破坏?尝试在函数声明中添加以下内容:extern "C" short *speccud(short ispecbu[])
  • 不,我检查过了,我也尝试了 extern "C" main.obj : error LNK2019: unresolved external symbol _speccud@4 referenced in function _main,结果出现了这个错误。几个星期以来一直坚持这个错误,迫切需要一些帮助。
  • 链接器正在寻找与 main.cpp 中的函数调用匹配的解包名称是 short * __stdcall speccud(short * const) 尝试将原型(在 main.cpp 中)和函数定义(在 wrapper.cu 中)更改为完全匹配。即将原型更改为short * __stdcall speccud(short * const);。将函数定义改为short * __stdcall speccud(short * const ispecbu)

标签: c++ cuda linker winusb


【解决方案1】:

链接器查看 main.cpp 所需的符号(即 main.obj 所需的入口点),并尝试在项目的其他模块中找到这些入口点的定义。

在 main.obj 的情况下,有一个符号(入口点)无法在别处定义:

1>main.obj : error LNK2019: unresolved external symbol "short * __stdcall speccud(short * const)" (?speccud@@YGPAFQAF@Z) referenced in function _main

在这种情况下,链接器说“我正在寻找这样定义的函数:

short * __stdcall speccud(short * const)

但我找不到!”。

当然,你没有在 main.cpp 中用__stdcall 装饰你的原型,这是由编译器在决定外部需要什么符号之前自动完成的。 VS C++ 编译器的行为方式多种多样,在这种情况下,它在编译 main.cpp 文件时的行为与其在编译 Wrapper.cu 时的(装饰)行为不同。结果,Wrapper.cu 中的函数 speccud 导出的名称与 main.obj 所识别的名称不同,因此链接器无法匹配它们。

因此,如果您通过使 Wrapper.cu 中的函数以与链接器所说的在 main.obj 中查找的完全匹配的方式导出来避免这种混淆,那么链接器可以找到匹配项:

链接器说,“我正在寻找:

    short * __stdcall speccud(short * const)

因此,如果您将原型更改为:

    short * __stdcall speccud(short * const);

函数定义为:

    short * __stdcall speccud(short * const ispecbu)

然后一切都匹配。

(在C++联动中,实际的匹配过程是根据mangled这个名字,就是这样的:

?speccud@@YGPAFQAF@Z

但错误报告过程也为我们解决了这个问题:

short * __stdcall speccud(short * const)

mangling/demangling 遵循已定义的过程,因此您可以使用 an online demangling service 将 mangled 名称转换为它的 demangled 版本。如果该函数没有 __stdcall 装饰,则该函数的损坏版本看起来会有所不同。)

【讨论】:

  • 当我使用 "extern "C" " 时,我最终得到了main.obj : error LNK2019: unresolved external symbol _speccud@4 referenced in function _main. 这里我没有错误的名称?speccud@@YGPAFQAF@Z,在这种情况下应该怎么做?(没有线索关于损坏的名称)。
  • 为什么不问一个新问题呢?我不知道你到底把extern "C" 放在哪里,很高兴看到完整而准确的编译器输出。我们不要试图在 cmets 的空间中涵盖所有这些。再加上其他人可能知道答案,新问题会得到更好的可见性。
猜你喜欢
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 2012-01-03
相关资源
最近更新 更多