Create node.js addon without node-waf

I am writing a simple node.js addon in C ++ using Eclipse CDT. There are many files in the project, and I would like to use the Eclipse managed assembly system.

I can compile a simple addon example with node-waf , but I cannot configure my Eclipse toolchain to create a proper shared library without waf. Waf uses gcc behind the scenes, so I'm sure this is possible.

What libs should I link to and what options should I go through to get it working?

I am currently getting the following error if I try to require my my lib:

 SyntaxError: Unexpected token ILLEGAL 
+6
source share
2 answers

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 

Import and Note:

The shared library must have a .node extension, for example: foobar.node

+8
source

I have not tried on Linux, but at least on OSX I had to use -undefined suppress and -flat_namespace , since node.js (v0.4.12) has its own statically linked v8 library in the executable.

The following Makefile compiles mod.cpp to mod.node on 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 

Run make:

 node app.js { hello: 'World' } 

Note: Source code mod.cpp from Addons

+3
source

Source: https://habr.com/ru/post/892563/


All Articles