Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind vg library with node.js nbind - how to set library and linking parameters in binding.gyp? #540

Closed
subwaystation opened this issue Nov 10, 2016 · 3 comments

Comments

@subwaystation
Copy link
Member

Hi vg team,

at the moment I am trying to build a JavaScript wrapper around your C++ library using https://github.com/charto/nbind.
However, I ran into some issues, see here https://github.com/charto/nbind/issues/35.

So at the moment I am stuck at the compilation step npm run -- node-gyp configure build. The problem is that the vg.hpp is not found:

  CXX(target) Release/obj.target/nbind/JS_VG.o
../JS_VG.cc:1:10: fatal error: 'vg.hpp' file not found
#include "vg.hpp"
         ^
1 error generated.

Here is my source file from which I want to invoke vg:

#include "vg.hpp"
#include "nbind/api.h"

namespace vg {
  // This inherits methods from VG for directly binding them when possible.
  class JS_VG : public VG {
    // Anything taking a callback will have to be wrapped!
    for_each_node(nbind::cbFunction lambda) { /* your code here */ }

    // Alternatively you can give your new implementation a new name.
    js_for_each_node(nbind::cbFunction lambda) { /* your code here */ }
  };
} // namespace

#include "nbind/nbind.h"

// You can rename your JS_VG reimplementation back to VG on the JavaScript side.
NBIND_CLASS(vg :: JS_VG, VG) {
  // You don't have to reimplement this, just tell nbind that it exists.
  method(normalize);

  // This C++ method is overloaded so we need to tell nbind which arguments it takes
  // (otherwise the C++ compiler cannot distinguish between overloads).
  multimethod(has_node, args(Node *));

  // If we want to bind several overloads, they need to be renamed uniquely
  // because nbind doesn't yet support overloading by type on the JavaScript side.
  multimethod(has_node, args(id_t), "has_node_id");

  // We've overloaded this with a new implementation.
  multimethod(for_each_node, args(nbind::cbFunction));

  // Alternatively if we renamed it uniquely, we can just rename it back here
  // and have nbind autodetect the arguments. Actually we can use camelCase
  // which is more common in JavaScript.
  method(js_for_each_node, "forEachNode");

  // Add an empty constructor
  construct<>();

  // Check if the graph is empty
  method(empty);

  // Generate has out of graph
  method(hash);
}

And here my binding.gyp file:

{
	"targets": [
		{
			"includes": [
				"auto.gypi"
			],
			"sources": [
				"JS_VG.cc"
			],
			"conditions": [
				["OS=='mac'", {
					"libraries": [
						"-lvg"
					]
				}],
			]
		}
	],
	"includes": [
		"auto-top.gypi"
	]
}

I tried putting libvg.a into the directory where I am building the stuff. Then I copied libvg.a into /usr/lib/libvg.a. All of this is not working. How do I have to set the compiler flags and the library stuff in order to compile it? Please help me out here.

For node-gyp see:
https://github.com/nodejs/node-gyp
https://gyp.gsrc.io/docs/InputFormatReference.md
https://gyp.gsrc.io/docs/UserDocumentation.md
https://github.com/nodejs/node-gyp/wiki/"binding.gyp"-files-out-in-the-wild

All the best.

@glennhickey
Copy link
Contributor

You compiler's complaining it can't find vg.hpp. You can fix this with the
-I flag to tell it where to look. For a simple example of a C++ project
that links against vg, you can look at the Makefile here as a very basic
example

https://github.com/glennhickey/ultrabubble_eval

(which relies on the VGDIR variable hardcoded in the Makefile).

On Thu, Nov 10, 2016 at 11:05 AM, subwaystation notifications@github.com
wrote:

Hi vg team,

at the moment I am trying to build a JavaScript wrapper around your C++
library using https://github.com/charto/nbind http://url.
However, I ran into some issues, see here https://github.com/charto/
nbind/issues/35 http://url.

So at the moment I am stuck at the compilation step npm run -- node-gyp
configure build. The problem is that the vg.hpp is not found:

CXX(target) Release/obj.target/nbind/JS_VG.o
../JS_VG.cc:1:10: fatal error: 'vg.hpp' file not found
#include "vg.hpp"
^
1 error generated.

Here is my source file from which I want to invoke vg:

#include "vg.hpp"
#include "nbind/api.h"
namespace vg {
// This inherits methods from VG for directly binding them when possible.
class JS_VG : public VG {
// Anything taking a callback will have to be wrapped!
for_each_node(nbind::cbFunction lambda) { /* your code here */ }

// Alternatively you can give your new implementation a new name.
js_for_each_node(nbind::cbFunction lambda) { /* your code here */ }

};
} // namespace

#include "nbind/nbind.h"
// You can rename your JS_VG reimplementation back to VG on the JavaScript side.NBIND_CLASS(vg :: JS_VG, VG) {
// You don't have to reimplement this, just tell nbind that it exists.
method(normalize);

// This C++ method is overloaded so we need to tell nbind which arguments it takes
// (otherwise the C++ compiler cannot distinguish between overloads).
multimethod(has_node, args(Node *));

// If we want to bind several overloads, they need to be renamed uniquely
// because nbind doesn't yet support overloading by type on the JavaScript side.
multimethod(has_node, args(id_t), "has_node_id");

// We've overloaded this with a new implementation.
multimethod(for_each_node, args(nbind::cbFunction));

// Alternatively if we renamed it uniquely, we can just rename it back here
// and have nbind autodetect the arguments. Actually we can use camelCase
// which is more common in JavaScript.
method(js_for_each_node, "forEachNode");

// Add an empty constructor
construct<>();

// Check if the graph is empty
method(empty);

// Generate has out of graph
method(hash);
}

And here my binding.gyp file:

{
"targets": [
{
"includes": [
"auto.gypi"
],
"sources": [
"JS_VG.cc"
],
"conditions": [
["OS=='mac'", {
"libraries": [
"-lvg"
]
}],
]
}
],
"includes": [
"auto-top.gypi"
]
}

I tried putting libvg.a into the directory where I am building the stuff.
Then I copied libvg.a into /usr/lib/libvg.a. All of this is not working.
How do I have to set the compiler flags and the library stuff in order to
compile it? Please help me out here.

For node-gyp see:
https://github.com/nodejs/node-gyp http://url
https://gyp.gsrc.io/docs/InputFormatReference.md http://url
https://gyp.gsrc.io/docs/UserDocumentation.md
https://github.com/nodejs/node-gyp/wiki/"binding.gyp"-
files-out-in-the-wild http://url

All the best.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#540, or mute the thread
https://github.com/notifications/unsubscribe-auth/AA2_7q0IoX22s3rlK_YmJ_T6IdbEQ8w2ks5q80DIgaJpZM4Ku1QW
.

@adamnovak
Copy link
Member

adamnovak commented Nov 10, 2016

In addition to copying libvg.a, a proper global install will need all the *.hpp files under vg/src and subfolders, and those under vg/include (after the build process populates it), to be somewhere in the global include directory (/usr/include, probably).

Unfortunately, in vg we have a bad habit of referring to our includes from dependencies without qualifying paths. So maybe you want to just dump them all into /usr/include/vg so they can find each other without gumming up the global include directory with things like "utils.h", and then pass a -I/usr/include/vg.

@subwaystation
Copy link
Member Author

The issue has been solved, for a short tutorial see here: charto/nbind#35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants