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

Posted

技术标签:

【中文标题】MSVS 错误 LNK2001:未解析的外部符号【英文标题】:MSVS error LNK2001: unresolved external symbol 【发布时间】:2014-08-20 11:57:17 【问题描述】:

我正在构建一个通过 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 ==========

【问题讨论】:

What is an undefined reference/unresolved external symbol error and how do I fix it? 的可能重复项 可能,@Niall,但哪一个是解决方案?我想不通.. :( 不确定。这些错误在寻找它们的时间里是臭名昭著的。尝试所有这些。尝试从 .lib 和 .dll 文件中转储导出,并在那里查找 CP210xRT_WriteLatch 函数,看看这些名称是否匹配。 “即使经过数小时的互联网研究”您忘记了 ,并且您忘记了 非互联网资源 .不要那么懒惰。 In Windows programming, the tell-tale sign that you did not link a necessary library is that the name of the unresolved symbol begins with __imp_. 【参考方案1】:

我很惊讶标头没有 C++“包装器”,因为它看起来像一个 C API。 也就是说,

#ifdef __cplusplus
extern "C"

#endif

在它的顶部,并且

#ifdef __cplusplus

#endif

在底部。

尝试添加这些。

【讨论】:

我会的。我会及时通知你 有点效果。我现在有一个不同的错误:error LNK2001: unresolved external symbol __imp__CP210xRT_WriteLatch@12 ,但看起来更有希望

以上是关于MSVS 错误 LNK2001:未解析的外部符号的主要内容,如果未能解决你的问题,请参考以下文章

错误 LNK2001:未解析的外部符号 WINAPI [重复]

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

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

从 C++ 连接 MySQL:错误 LNK2001:未解析的外部符号 [重复]

为啥错误 LNK2001:在这种情况下无法解析外部符号? [复制]

radial.o:错误 LNK2001:无法解析的外部符号 lambda_fatal 错误 LNK1120:8 个无法解析的外部,error.failed 退出状态为 1120