How to Link C++ DLL, Header, and Lib Files to a Node-Gyp Addon

Anthony A. Vardaro, Dec 2020

I was running into an issue linking a Windows-based C++ SDK to a node-gyp module. I wanted to wrap the SDK into a node-gyp addon and reference it from a Node process. Surprisingly, there is no documentation on getting node-gyp to recognize DLLs at runtime. I decided to write down my findings in hopes that I can save someone from a lot of Googling.

The library I was working with came packaged with header files, DLLs, and .lib files that needed to be imported into my C++ addon. You need to reference these files in your binding.gyp file, and also copy the DLLs to the target build directory of your addon.

Including the Header Files

You need to expose the directory containing your header files in your binding.gyp. For me, they are within a subdirectory of my addon, so I can use the handy module_root_dir environment variable to reference it.


{
    "include_dirs": [
        "<(module_root_dir)/tobii/include"
    ],
}
            
Linking the .lib Files

You also need to tell node-gyp where it can find the .lib files in your library.


{
    "libraries": [
        "<(module_root_dir)/tobii/lib/x64/tobii_interaction_lib.lib",
        "<(module_root_dir)/tobii/lib/x64/tobii_stream_engine.lib"
    ],
}
            
Copy the DLLs to Your Target Build Folder

Specify where the DLLs are, and where to copy them at build. In my case, I'm building to ./build/Release/. My addon.node target and both of my defined DLLs will be written to this directory.


{
    "copies": [
        {
        "destination": "<(module_root_dir)/build/Release/",
            "files": [
                "<(module_root_dir)/tobii/lib/x64/tobii_interaction_lib.dll",
                "<(module_root_dir)/tobii/lib/x64/tobii_stream_engine.dll"
            ]
        },
    ]
}
            
The Finished binding.gyp

Here is final product of the binding.gyp file.


{
  "targets": [
    {
      "target_name": "addon",
      "sources": [
        "main.cc"
      ],
      "conditions": [
        [
          "OS==\"win\"",
          {
            "libraries": [
              "<(module_root_dir)/tobii/lib/x64/tobii_interaction_lib.lib",
              "<(module_root_dir)/tobii/lib/x64/tobii_stream_engine.lib"
            ],
            "include_dirs": [
              "<(module_root_dir)/tobii/include"
            ],
            "copies": [
              {
                "destination": "<(module_root_dir)/build/Release/",
                "files": [
                  "<(module_root_dir)/tobii/lib/x64/tobii_interaction_lib.dll",
                  "<(module_root_dir)/tobii/lib/x64/tobii_stream_engine.dll"
                ]
              }
            ]
          }
        ]
      ]
    }
  ]
}
            
Github Email LinkedIn