Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a third party dll, header, and lib file with Node N-API

I have been given all of the appropriate files to use a c++ dll:

  • header file
  • lib file
  • dll

I am attempting to use Node N-API with the given files so we can use this c++ dll in our node server.

The problem is that when I try to build with node-gyp it throws the following error:

LINK : fatal error LNK1181: cannot open input file 'lib\MathUtils.lib' [C:\Development\Github\node-thin-client\service\build\interface.vcxproj]

The node version I am using is: 8.11.2

The node-gyp version is: 3.6.2

my binding.gyp file is as follows:

{
  "variables": {
    "dll_files": [
      "lib/MathUtils.dll"
    ]
  },
  "targets": [
    {
      "target_name": "interface",
      "sources": [
        "src/interface/interface.cpp"
      ],
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")",
        "include"
      ],
      "dependencies": [
        "<!(node -p \"require('node-addon-api').gyp\")"
      ],
      "libraries": [
        "lib/MathUtils.lib"
      ],
      "cflags!": ["-fno-exceptions"],
      "cflags_cc!": ["-fno-exceptions"],
      "defines": ["NAPI_CPP_EXCEPTIONS"]
    }
  ]
}
like image 599
mlangwell Avatar asked Jun 05 '18 20:06

mlangwell


1 Answers

I ran into similar problems. I see it's been a while but in case anyone else runs into this while building a node plugin that links to other dlls, here is what I did:

You are getting the link error because the project file is generated in the ./build directory under your project, but you referenced the ./lib/MathUtils.lib. If you opened the generated .sln project in Visual Studio you would see the problem. So you can do:

"libraries": [../lib/MathUtils.lib"] 

or

"libraries": [ "<(module_root_dir)/lib/MathUtils.lib" ]

Note, first the relative path goes up one directory. The second one will put the full path in the linker line of the project.

Also for me the variables section did not copy the dll to the release directory, it seemed to do nothing. Instead I used a copies section:

     "conditions": [        
        ["OS==\"win\"", {
          "libraries": [ "<(module_root_dir)/tsflexnet/TSFlexnetCLib.lib" ],
          "copies": [
            {
              "destination": "<(module_root_dir)/build/Release/",
              "files": [ "<(module_root_dir)/tsflexnet/TSFlexnetCLib.dll" ]
            }
          ]
        }]
      ]
like image 141
Greg9504 Avatar answered Oct 03 '22 08:10

Greg9504