Build a Node.JS C++ Module
Prerequisites
nvm
Build a Node.JS C++ Module from Scratch
nvm use 8
npm install -g node-gyp
mkdir addon
cd addon
npm init
touch binding.gyp
Edit binding.gyp and write this inside
{
"targets": [
{
"target_name": "addon",
"sources": [ "addon.cc" ]
}
]
}
touch addon.cc
Edit addon.cc and write this inside
#include <node.h>
#include <iostream>
using namespace v8;
using namespace std;
void HelloWorld(const FunctionCallbackInfo<Value>& args) {
cout << "Hello, world!" << endl;
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "helloWorld", HelloWorld);
}
NODE_MODULE(addon, Initialize);
node-gyp configure build
Troubleshooting
Problem:
/usr/bin/ld: skipping incompatible /home/frank/kraken/vfcore/protobuf/arch/CentOS7-linux-i686/protobuf-2.4.1/stage/lib/libprotobuf.a when searching for -lprotobuf
Solution:
The library is 32-bit, but the node.js addon build is 64-bit.
You can check the architecture of your .a with :
objdump -f /home/frank/protobuf/arch/CentOS7-linux-i686/protobuf-2.4.1/stage/lib/libprotobuf.a | grep architecture
architecture: i386, flags 0x00000011:
architecture: i386, flags 0x00000010:
architecture: i386, flags 0x00000011:
architecture: i386, flags 0x00000011:
architecture: i386, flags 0x00000011:
(...)
So the solution is to change the include dir to
"-L/home/frank/protobuf/arch/CentOS7-linux-x86_64/protobuf-2.4.1/stage/lib",
Problem :
/usr/bin/ld: /home/frank/kraken/vfcore/protobuf/arch/CentOS7-linux-x86_64/protobuf-2.4.1/stage/lib/libprotobuf.a(common.o): relocation R_X86_64_32S against symbol `_ZTVN6google8protobuf7ClosureE' can not be used when making a shared object; recompile with -fPIC
Recent Comments