Build node.js addon without node-waf
I'm writing a simple node.js addon in C++ using Eclipse CDT. The project has many files and I'd like to use the Eclipse's managed build system.
I can compile a simple addon example with node-waf
, but I can't configure my Eclipse toolchain to build a proper shared library without waf. Waf uses gcc behind the scenes, so I'm sure it's possible.
Which libs should I link to and what kind of options should I pass along to make it work?
Currently I get the following error if I try to require
my lib:
SyntaxError: Unexpected 开发者_Go百科token ILLEGAL
Finally found the answer.
Required compiler flags:
g++
-g
-fPIC
-DPIC
-D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64
-D_GNU_SOURCE
-DEV_MULTIPLICITY=0
-I/usr/local/include/node
addon.cc
-c
-o addon.o
Linker flags:
g++ addon.o -o addon.node -shared -L/usr/local/lib
Importand note:
The shared library must have the extension .node
, e.g: foobar.node
I haven't tried in Linux but at least in OSX I had to use -undefined suppress
and -flat_namespace
since node.js(v0.4.12) has it's own statically linked v8 library in the executable.
The following Makefile compiles mod.cpp into mod.node in MacOSX Lion:
all: mod.node
node app.js
mod.o: mod.cpp
g++ -g -fPIC -DPIC -D_LARGEFILE_SOURCE -m64 -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -DEV_MULTIPLICITY=0 -I/usr/local/include/node mod.cpp -c -o mod.o
mod.node: mod.o
g++ -flat_namespace mod.o -o mod.node -undefined suppress -bundle -L/usr/local/lib
clean:
rm mod.o
rm mod.node
$ file mod.o
mod.o: Mach-O 64-bit object x86_64
$ file mod.node
mod.node: Mach-O 64-bit bundle x86_64
Running make:
node app.js
{ hello: 'World' }
Note: The source code of mod.cpp it's from the Addons tutorial
精彩评论