1

I am building a c++ wrapper to javascript in swig. Created example.i file compiled and created example_wrap.cxx file, then while compiling with node-gyp it gives build error.

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

Please help ! Thank you :)

  1. example.i
%module example

%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}
  1. ran following command results in generation on example_wrap.cxx file.
swig -javascript -node -c++ example.i
  1. binding.gyp
{
  "targets": [
    {
      "target_name": "example",
      "sources": [ "example.cxx", "example_wrap.cxx" ]
    }
  ]
}
  1. Building gives above error.
node-gyp configure build

1 Answers1

0

I think I see the problem here, although I've not got a setup that lets me easily test this out, so your millage may vary.

In your bindings.gyp you have this:

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

Essentially this is saying that you're going to compile two .cxx files, one named example.cxx and one named example_wrap.cxx.

The error message from your build attempt suggests that it doesn't know how to build 'example.o', which is almost certainly meant to be generated from 'example.cxx'.

My strong suspicion is that you don't have a file named example.cxx in your project. And based on what you wrote in the SWIG interface file (example.i) you could avoid needing one anyway as you've used %inline.

So based on that you've got two options I think.

  • Firstly you could remove example.cxx from your binding.gyp file and add an definition of your wrapped things there instead of making them extern. So your .i file becomes something like this:

    %module example
    
    %inline %{
    int gcd(int x, int y) {
      // TODO: your code goes here...
    }
    double Foo;
    %}
    
  • Secondly you could create an example.cxx file. Your .i file remains unchanged. You would then need to add some code into that example.cxx file:

    // 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;
    
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Thank you so much for your answer. But if I don't use example.cxx it gives an error - return process.dlopen(module, path.toNamespacedPath(filename)); ^ Error: /home/snehabhapkar/Videos/swig/example1/build/Release/example.node: undefined symbol: Foo –  Jul 31 '19 at 04:45
  • Works fine if I include example.cxx too –  Jul 31 '19 at 04:46