0

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 ==========
Sten Wessel
  • 95
  • 2
  • 9
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Niall Aug 20 '14 at 12:24
  • Possibly, @Niall, but which one is the solution? I can't figure it out.. :( – Sten Wessel Aug 20 '14 at 12:45
  • Not sure. These errors are notorious in the time to take to find them. Try all of them. Try dump the exports from the .lib and .dll files and look for the `CP210xRT_WriteLatch` function there, look to see if the mangled names match. – Niall Aug 20 '14 at 12:50
  • _"Even after hours of internet researching"_ You forgot to do **days**, and you forgot **non-internet sources**. Don't be so lazy. – Lightness Races in Orbit Aug 20 '14 at 13:06
  • [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_`.](http://stackoverflow.com/a/12574400/902497) – Raymond Chen Aug 20 '14 at 18:03

1 Answers1

1

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.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82