链接 Winusb 应用程序和 CUDA 时出现链接器错误 2019

Linker error 2019 while linking Winusb application and CUDA

本文关键字:链接 错误 2019 Winusb 应用程序 CUDA      更新时间:2023-10-16

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

构建日志 :

1>------ Build started: Project: USB Application2, Configuration: Win7 Debug Win32 ------
1>  Compiling CUDA source file function.cu...
1>  
1>  C:Usersbel1documentsvisual studio 2012ProjectsUSB Application2USB Application2>"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5binnvcc.exe" -gencode=arch=compute_10,code=sm_10 --use-local-env --cl-version 2010 -ccbin "C:Program FilesMicrosoft Visual Studio 11.0VCbin"  -IWin7Debug -IWin7Debug -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include" -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include" -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include"     --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 Win7Debugfunction.cu.obj "C:Usersbel1documentsvisual studio 2012ProjectsUSB Application2USB Application2function.cu" 
1>  Compiling CUDA source file wrapper.cu...
1>  
1>  C:Usersbel1documentsvisual studio 2012ProjectsUSB Application2USB Application2>"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5binnvcc.exe" -gencode=arch=compute_10,code=sm_10 --use-local-env --cl-version 2010 -ccbin "C:Program FilesMicrosoft Visual Studio 11.0VCbin"  -IWin7Debug -IWin7Debug -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include" -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include" -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.5include"     --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 Win7Debugwrapper.cu.obj "C:Usersbel1documentsvisual studio 2012ProjectsUSB Application2USB Application2wrapper.cu" 
1>main.obj : error LNK2019: unresolved external symbol "short * __stdcall speccud(short * const)" (?speccud@@YGPAFQAF@Z) referenced in function _main
1>C:Usersbel1documentsvisual studio 2012ProjectsUSB     Application2Win7DebugUSBApplication2.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

主.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 installedn"));
    } else {
       printf(_T("Failed looking for device, HRESULT 0x%xn"), 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 %dn"),
           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,"%dtn",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,"%dtn",fans[k]);
    //tempc[k]=specbu[k];
}
std::getchar();
CloseDevice(&deviceData);
return 0;
}

Wrapper.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;
 }

Function.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

我还有一个设备.cpp它具有打开设备,关闭设备等功能(Winusb的一部分,并且有太多行自动生成的代码)。它看起来像 Cuda 和 Winusb 应用程序之间的某种链接器错误。当我简单地创建两个视觉工作室项目时,一个用于 Cuda,另一个用于 winusb,两者都完美运行。

链接器查看 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++编译器的行为方式多种多样,在这种情况下,它在编译主文件时的行为与编译 Wrapper.cu 时的行为不同.cpp其(装饰)行为。 因此,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++联动中,实际的匹配过程是基于被篡改的名称,即:

?speccud@@YGPAFQAF@Z

但是错误报告过程也为我们破坏了这一点:

short * __stdcall speccud(short * const)

重整/拆解遵循定义的过程,因此您可以使用在线拆解服务将拆解名称转换为其拆解版本。 如果此函数没有__stdcall修饰,则此函数的损坏版本看起来会有所不同。