将string参数从node.js传递给C ++代码(使用SWIG)

我想调用一个用Javascript代码传递string参数的C ++编写的方法。 我find的解决scheme是使用Node.js和SWIG来生成绑定。

我遵循这里的例子(请参阅enobayram post): 如何使用node.js中的C ++库?

C ++方法如下所示:

Handle<Value> CMyClass::Open(const v8::Arguments& args) { HandleScope scope; std::string port(*v8::String::Utf8Value(args[0])); std::cout << port << std::endl; return scope.Close(Undefined()); } 

从Node.js控制台:

 var toto = require("./build/Release/MyClass") var c = new toto.CMyClass() c.Open("test") Error: in method 'CMyClass_Open', argument 2 of type 'v8::Arguments const &' at repl:1:4 at REPLServer.self.eval (repl.js:110:21) at Interface.<anonymous> (repl.js:239:12) at Interface.EventEmitter.emit (events.js:95:17) at Interface._onLine (readline.js:202:10) at Interface._line (readline.js:531:8) at Interface._ttyWrite (readline.js:760:14) at ReadStream.onkeypress (readline.js:99:10) at ReadStream.EventEmitter.emit (events.js:98:17) at emitKey (readline.js:1095:12) 

有任何想法吗 ? 非常感谢

在你的swig_wrapper.i(或者例子中的mylib.i)中,在%include myclass.h的行之前添加%include "std_string.i" ,然后再次运行swig。

引文: http : //www.swig.org/Doc1.3/Library.html#Library_stl_cpp_library

 std::deque std_deque.i std::list std_list.i std::map std_map.i std::pair std_pair.i std::set std_set.i std::string std_string.i std::vector std_vector.i 

C ++示例:

 /* C++ --> JavaScript */ std::string testOut() { return "Hello World!"; } /* JavaScript --> C++ */ void testIn(std::string args) { std::cout << "Result: " << args << std::endl; }