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

时间:2014-11-18 16:19:22

标签: c++ cuda linker winusb

我有两个应用程序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;
}

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

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

1 个答案:

答案 0 :(得分:0)

链接器查看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

在这种情况下,链接器说&#34; 我正在寻找一个像这样定义的函数:

short * __stdcall speccud(short * const)

但我无法找到它!&#34;。

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

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

链接器说,&#34; 我正在寻找:&#34;

    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将受损的名称转换为它的解码版本。如果函数没有__stdcall装饰,则此函数的损坏版本看起来会有所不同。)