我如何运行 node-gyp?

how I run node-gyp?

我关注这个link:http://www.swig.org/Doc3.0/Javascript.html#Javascript_running_swig 为了使 nodejs extension.So 我创建了一个包含 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" ]
    }
  ]
}

我运行这个命令:

sudo npm install -g node-gyp

然后:

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

然后:

node-gyp configure

当我 运行 构建 node-gyp 时,我遇到了这些错误:

    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 

我在这个主题上找到了很多帖子,但它对我不起作用,我 运行 npm -g install node-gyp 之后我 运行 node-gyp-build 但它不起作用没用。

binding.gyp指的是两个源文件:

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

example_wrap.cxx 由 Swig 生成,另一个包含您的代码。您似乎将该文件命名为 example.c,因此 node-gyp 找不到它。解决方案:将 example.c 重命名为 example.cxx.

由于您使用的是 Node 0.12,接下来您可能 运行 会遇到一堆复杂的错误。如果是这样,请在编辑器中打开 example_wrap.cxx,搜索 SWIG_V8_VERSION 并将定义替换为:

#define SWIG_V8_VERSION 0x032873

您可以查看 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 而不是 Swig,这是一个好的开始)。分发使用 nan 构建的插件也更容易,因为所有依赖项都在常规 npm install 过程中安装(而使用 Swig,您必须依赖它在安装模块本身之前安装)。