I am trying to create my first npm addon, but I keep getting an unresolved external symbol.
> node-gyp configure build
> error LNK2001: unresolved external symbol DeliverMessage
The error is telling me that the header file is not being linked to the library file. I know the binding.gyp file finds the library file because it is not throwing a "cannot open input file". Everything seems correct and I am not really sure what to check next.
using: node v8.0.0
node-gyp v3.6.2
project structure:
- example
- myLibrary.h
- myLibrary.lib
- example.cc
- binding.gyp
// myLibrary.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
int Send(char *Address1, unsigned short SaveLoad, char *Address2);
// example.cc
#include "myLibrary.h"
#include <node.h>
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Value;
void SendMessage (const FunctionCallbackInfo<Value>& args) {
char *a = "AAAAAA";
char *c = "CCCCCC";
int result = DeliverMessage(a, 0, c);
args.GetReturnValue().Set(result);
}
void Init(Local<Object> exports) {
NODE_SET_METHOD(exports, "sendMessage", SendMessage);
}
NODE_MODULE(addon, Init)
// binding.gyp
{
"targets": [
{
"target_name": "exampleBuild",
"link_settings": {
"libraries": [
"../myLibrary.lib",
]
},
"sources": [ "example.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")",
]
}
]
}
After a lot of time of troubleshooting and researching, I found that I had two problems:
(1) The .lib file is written in C. So I needed to encapsulate my header file like so:
(2) The .lib was 32-bit and my version of node was 64-bit. I had to install 32-bit node.
When the addon was built, it created a bindings.sln file. This file was extremely helpful in figuring out the problem. I opened it in Visual Studio, which offered some warnings and errors.