-1

I follow this link:http://www.swig.org/Doc3.0/Javascript.html#Javascript_running_swig to make nodejs extension.So I create a folder test which contain example.c

    #include <time.h>
 double My_variable = 3.0;

 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }

 int my_mod(int x, int y) {
     return (x%y);
 }

 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

example.i:

 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}

 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();

binding.gyp:

    {
  "targets": [
    {
      "target_name": "example",
      "sources": [ "example.cxx", "example_wrap.cxx" ]
    }
  ]
}

I run this command:

sudo npm install -g node-gyp

Then:

swig -javascript -node -c++ example.i

then:

node-gyp configure

when I run node-gyp build I got these errors:

    gyp info it worked if it ends with ok
gyp info using node-gyp@2.0.2
gyp info using node@0.12.7 | linux | ia32
gyp info spawn make
gyp infomake: Entering directory `/home/slim/test/build'
make: *** No rule to make target `Release/obj.target/example/example.o', needed by `Release/obj.target/example.node'.  Stop.
make: Leaving directory `/home/slim/test/build'
 spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/node-gyp/lib/build.js:269:23)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1074:12)
gyp ERR! System Linux 3.13.0-32-generic
gyp ERR! command "node" "/usr/bin/node-gyp" "build"
gyp ERR! cwd /home/slim/test
gyp ERR! node -v v0.12.7
gyp ERR! node-gyp -v v2.0.2
gyp ERR! not ok 

I found many posts in this subject but it doesn't work for me,I run npm -g install node-gyp and after I run node-gyp-build but it doesn't work.

arrow man
  • 51
  • 2
  • 11

1 Answers1

2

binding.gyp refers to two source files:

"sources": [ "example.cxx", "example_wrap.cxx" ]

example_wrap.cxx is generated by Swig, the other one contains your code. It seems that you have named that file example.c instead, so node-gyp can't find it. Solution: rename example.c to example.cxx.

Since you're using Node 0.12, you may run into a bunch of complication errors next. If so, open example_wrap.cxx in your editor, search for SWIG_V8_VERSION and replace the definition to this:

#define SWIG_V8_VERSION 0x032873

Instead of Swig, you could look into nan, which is an alternative way of creating Node.js addons. It works well (I have experience with it) and there's quite a lot of example code available (node-native-boilerplate is a good start). It's also much easier to distribute addons built with nan because all dependencies are installed during the regular npm install process (whereas with Swig you have to rely on it being installed before the module itself can be installed).

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • I just found this after running into those "bunch of complication erros" you mentioned :) As you flagged, using`#define SWIG_V8_VERSION 0x032873` fixes these issues. What is going on here though? Why is it necessary to put this flag here, and is there some way to make it automatic? – George Mar 26 '16 at 00:08
  • After playing with swig/node.js for a bit, it appears that swig cannot handle C++ functions that return arrays (more precisely: functions that return pointers to arrays). That is, it seems you cannot build a function in C++ that returns something like a JavaScript array and have swig automatically convert it for you. – George Mar 26 '16 at 03:12
  • @George I believe (can't remember specifically) that setting `SWIG_V8_VERSION` to that particular value makes Swig output code that compiles properly on Node 0.12 (and perhaps later versions, too). Usually, the V8 C++ semantics change between significant Node.js updates (0.10 → 0.12 → 4.x → ...), so the outputted code needs to be adjusted. I don't use Swig so no idea if it can be automated. – robertklep Mar 26 '16 at 07:13