'Using 3rd party lib file in NodeJs and getting LNK2001 error

I have been searching for an answer for a while and couldn't find anything that worked. Here is some background: I am trying to build an electron app that can use an OpenDMX interface. I wanted to direct the problem into smaller problems so I first tried to get the basic code working in c++, which I did. Next step is to implement that c++ code into NodeJs before even messing with any electron modules and here is where I am getting my problems. I am trying to write a Native NodeJs Module that can communicate with the OpenDMX device so my js can call those functions. I got it to the point where including the lib file works but as soon as I try to call a function from that lib file, it thorws error messages. Here is the code and the error messages:

opendmx.cc (I commented out some code for debugging)

#include "../header/opendmx.h"

// byte buffer[513];
// int bufferLength = 513;
// FT_HANDLE handle;
// bool done = false;
// bool connected = false;
// unsigned long bytesWritten = 0;
// FT_STATUS status;

// byte BITS_8 = 8;
// byte STOP_BITS_2 = 2;
// byte PARITY_NONE_BYTE = 0;
// unsigned short FLOW_NONE = 0;
// byte PURGE_RX = 1;
// byte PURGE_TX = 2;

int initPickDevice(){
    unsigned long deviceCount = 0;
    if (FT_CreateDeviceInfoList(&deviceCount) != FT_OK)
    {
        printf("Unable to query devices. Exiting.\r\n");
        return 1;
    }

    //get a list of information about each FTDI device
    FT_DEVICE_LIST_INFO_NODE *deviceInfo = (FT_DEVICE_LIST_INFO_NODE *)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE) * deviceCount);
    if (FT_GetDeviceInfoList(deviceInfo, &deviceCount) != FT_OK)
    {
        printf("Unable to get the lisit of info. Exiting.\r\n");
        return 1;
    }

    for (unsigned long i = 0; i < deviceCount; i++)
    {
        // printf("Device = %d\r\n", i);
        // printf("Flags = 0x%X\r\n", deviceInfo[i].Flags);
        // printf("Type = 0x%X\r\n", deviceInfo[i].Type);
        // printf("ID = 0x%X\r\n", deviceInfo[i].ID);
        // printf("LocId = 0x%X\r\n", deviceInfo[i].LocId);
        // printf("SN = %s\r\n", deviceInfo[i].SerialNumber);
        // printf("Description = %s\r\n", deviceInfo[i].Description);
        // printf("Handle = %s\r\n", deviceInfo[i].ftHandle);
        // printf("\r\n");
    }

    return 0;
}

NAN_METHOD(opendmx){
    initPickDevice();
}

NAN_MODULE_INIT(init)
{
    Nan::SetMethod(target, "opendmx", opendmx);
}

NODE_MODULE(opendmx, init);

opendmx.h

#pragma once
#ifndef OPENDMX_H
#define OPENDMX_H

#define FTD2XX_STATIC
#include "../header/ftd2xx.h"
#include <iostream>
#include <nan.h>
// using namespace v8;

#endif

binding.gyp

{
    "targets": [
        {
            "target_name": "opendmx",
            "sources": ["src/opendmx.cc"],
            "include_dirs": ["<!(node -e \"require('nan')\")"],
            "includePath": [
                "${workspaceFolder}/**",
                "${env:USERPROFILE}/AppData/Local/node-gyp/Cache/14.15.0/include/node"
            ],
            "libraries": ["../lib/ftd2xx.lib"]
        }
    ]
}

main.js

require("nan");
console.log('finished "nan"');
const { opendmx } = require("./build/Release/opendmx.node");
console.log('finished "opendmx"');

const cPlusPlusHelloTime = "c++ opendmx";
console.time(cPlusPlusHelloTime);
opendmx();
console.timeEnd(cPlusPlusHelloTime);

Error Message:

Creating library C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\Release\opendmx.lib and object C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\Release\opendmx.exp
opendmx.obj : error LNK2001: unresolved external symbol FT_CreateDeviceInfoList [C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\opendmx.vcxproj]
opendmx.obj : error LNK2001: unresolved external symbol FT_GetDeviceInfoList [C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\opendmx.vcxproj]
C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\Release\opendmx.node : fatal error LNK1120: 2 unresolved externals [C:\Users\david\Documents\GitHub\2021\FTDI\FTDI Test\build\opendmx.vcxproj]

As far as I researched, the lib file is not linked, but when I remove the library tag in the bindings.gyp, it gives me a different error message, telling me that its not linked. So I guess its supposed to be linked in the way that I did it.

EDIT: I just found something, apparently, gyp works slightly different than the c++ compiler that you can find in VS. Suppositly i am supposed to add 'extern "C"' to the header file, which I tried in from of the parts where it is throwing an error but its not really solving anything. Just getting the same error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source