使用 swig 将 C++ 的包装器创建为 javascript 时生成错误

Build error while creating wrapper for C++ to javascript using swig

我正在构建一个 javascript 的 c++ 包装器。 Created example.i file compiled and created example_wrap.cxx file, then while compiling with node-gyp it gives build error.

错误:

make: Entering directory '/home/snehabhapkar/Videos/swig/example1/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/snehabhapkar/Videos/swig/example1/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:196:23)
gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Linux 5.0.9-301.fc30.x86_64
gyp ERR! command "/usr/bin/node" "/usr/bin/node-gyp" "configure" "build"
gyp ERR! cwd /home/snehabhapkar/Videos/swig/example1
gyp ERR! node -v v10.16.0
gyp ERR! node-gyp -v v5.0.3
gyp ERR! not ok

请帮忙!谢谢:)

  1. example.i
%module example

%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}
  1. 运行 以下命令会生成 example_wrap.cxx 文件。
swig -javascript -node -c++ example.i
  1. binding.gyp
{
  "targets": [
    {
      "target_name": "example",
      "sources": [ "example.cxx", "example_wrap.cxx" ]
    }
  ]
}
  1. 构建给出上述错误。
node-gyp configure build

我想我看到了这里的问题,虽然我没有可以让我轻松测试它的设置,所以你的 millage 可能会有所不同。

在你的 bindings.gyp 你有这个:

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

基本上这就是说您要编译两个 .cxx 文件,一个名为 example.cxx 和一个名为 example_wrap.cxx.

您构建尝试的错误消息表明它不知道如何构建 'example.o',这几乎可以肯定是从 'example.cxx'.

生成的

我强烈怀疑您的项目中没有名为 example.cxx 的文件。根据您在 SWIG 接口文件 (example.i) 中编写的内容,您可以避免使用 %inline.

基于此,我认为您有两个选择。

  • 首先,您可以从 binding.gyp 文件中删除 example.cxx 并在其中添加包装内容的定义,而不是使它们成为 extern。所以你的 .i 文件变成这样:

    %module example
    
    %inline %{
    int gcd(int x, int y) {
      // TODO: your code goes here...
    }
    double Foo;
    %}
    
  • 其次,您可以创建一个 example.cxx 文件。您的 .i 文件保持不变。然后,您需要在该 example.cxx 文件中添加一些代码:

    // implementations of things that SWIG is going to wrap for us go here
    int gcd(int x, int y) {
      // TODO: return some answer here...
    }
    double Foo;