MSVS错误LNK2001:未解析的外部符号

时间:2014-08-20 11:57:18

标签: c++ visual-studio linker lnk2001

我正在构建一个通过COM端口连接打开/关闭中继的简单项目。即使经过数小时的互联网研究,我也无法找到解决链接错误的方法:

1>------ Build started: Project: relayon, Configuration: Release Win32 ------
1>  relayon.cpp
1>relayon.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall CP210xRT_WriteLatch(void *,unsigned char,unsigned char)" (__imp_?CP210xRT_WriteLatch@@YGHPAXEE@Z)
1>C:\Users\Sten\Documents\Visual Studio 2010\Projects\relayon\Release\relayon.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我的代码:

CP210xRuntimeDLL.h(来自Silicon Labs)

#include <windows.h>

#ifdef CP210xRUNTIMEDLL_EXPORTS 
#define CP210xRUNTIMEDLL_API __declspec(dllexport) 
#else 
#define CP210xRUNTIMEDLL_API __declspec(dllimport)
#endif 

#define     CP210x_MAX_SETUP_LENGTH                 65536 

#ifndef _CP210x_STANDARD_DEF_ 
#define _CP210x_STANDARD_DEF_ 
// GetDeviceVersion() return codes 
#define     CP210x_CP2101_VERSION               0x01 
#define     CP210x_CP2102_VERSION               0x02 
#define     CP210x_CP2103_VERSION               0x03 

// Return codes                                  
#define     CP210x_SUCCESS                      0x00 
#define     CP210x_DEVICE_NOT_FOUND             0xFF 
#define     CP210x_INVALID_HANDLE               0x01 
#define     CP210x_INVALID_PARAMETER            0x02 
#define     CP210x_DEVICE_IO_FAILED             0x03 
#define     CP210x_FUNCTION_NOT_SUPPORTED       0x04 
#define     CP210x_GLOBAL_DATA_ERROR            0x05 
#define     CP210x_COMMAND_FAILED               0x08 
#define     CP210x_INVALID_ACCESS_TYPE          0x09 

// Type definitions 
typedef     int     CP210x_STATUS; 
#endif /*_CP210x_STANDARD_DEF_*/ 

// Mask and Latch value bit definitions 
#define     CP210x_GPIO_0                       0x01 
#define     CP210x_GPIO_1                       0x02 
#define     CP210x_GPIO_2                       0x04 
#define     CP210x_GPIO_3                       0x08 

CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI 
CP210xRT_ReadLatch( HANDLE cyHandle, 
                LPBYTE  lpbLatch); 

CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI 
CP210xRT_WriteLatch(    HANDLE cyHandle, 
                BYTE    bMask, 
                BYTE    bLatch); 


CP210xRUNTIMEDLL_API CP210x_STATUS WINAPI 
CP210xRT_GetPartNumber( HANDLE cyHandle, 
                            LPBYTE  lpbPartNum); 

relayon.cpp

#include "CP210xRuntimeDLL.h"
#include <windows.h>

int main()
{
    // String for COM port
    LPCWSTR comport = L"\\\\.\\COM3";

    // Create handle for COM
    HANDLE hCOM = CreateFile(comport,
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
        0);

    // Purge COM (enable read/write and clear io buffers)
    PurgeComm(hCOM, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

    // Save COM origional state
    DCB dcbCOMInitialState;
    GetCommState(hCOM, &dcbCOMInitialState);

    // Set new state
    DCB dcbCOM = dcbCOMInitialState;

    dcbCOM.BaudRate = 57600;
    dcbCOM.Parity = NOPARITY;
    dcbCOM.ByteSize = 8;
    dcbCOM.StopBits = ONESTOPBIT;

    SetCommState(hCOM, &dcbCOM);

    Sleep(60);

    // WRITE !
    CP210xRT_WriteLatch(hCOM, 15, 0);
    Sleep(2000);
    CP210xRT_WriteLatch(hCOM, 15, 1);

    // Close COM (first back to initial state)
    SetCommState(hCOM, &dcbCOMInitialState);
    Sleep(60);

    CloseHandle(hCOM);
    hCOM = INVALID_HANDLE_VALUE;

    return 0;
}

有人可以帮我吗?它让我发疯了!

提前致谢,

斯登

编辑1 molbdnilo提供的解决方案为错误工作,但提出了另一个同类错误:

1>------ Build started: Project: relayon, Configuration: Release Win32 ------
1>  relayon.cpp
1>relayon.obj : error LNK2001: unresolved external symbol __imp__CP210xRT_WriteLatch@12
1>C:\Users\Sten\Documents\Visual Studio 2010\Projects\relayon\Release\relayon.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:1)

我很惊讶标题没有C ++&#34;包装&#34;,因为它看起来像C API。
也就是说,

#ifdef __cplusplus
extern "C"
{
#endif

位于其顶部,

#ifdef __cplusplus
}
#endif

在底部。

尝试添加它们。