Sten Wessel
Sten Wessel

Reputation: 195

MSVS error LNK2001: unresolved external symbol

I am building a simple project that opens/closes a relay through a COM port connection. Even after hours of internet researching I couldn't find a solution to the linking error I get:

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 ==========

This is my code:

CP210xRuntimeDLL.h (from 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;
}

Can someone help me with this? It drives me crazy!

Thanks in advance,

Sten

EDIT 1 The solution provided by molbdnilo worked for the error, but brought up another one of the same kind:

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 ==========

Upvotes: 0

Views: 1009

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

I'm surprised that the header doesn't have a C++ "wrapper", as it looks like a C API.
That is,

#ifdef __cplusplus
extern "C"
{
#endif

at the top of it, and

#ifdef __cplusplus
}
#endif

at the bottom.

Try adding those.

Upvotes: 1

Related Questions