nodejs addon错误:无法加载共享库

我有这个简单的节点插件的代码:

#include <node.h> #include <v8.h> #include <string> #include <vector> template <class S> class FooString { protected: static std::vector< S > vec_strings; public: const S &str() const { return vec_strings[0]; } std::string tostdstring() const; }; template <> std::string FooString<std::string>::tostdstring() const { return str(); } namespace v8 { Handle<Value> Method(const Arguments& args) { HandleScope scope; return scope.Close(String::New("world")); } void init(Handle<Object> target) { target->Set(String::NewSymbol("hello"), FunctionTemplate::New(Method)->GetFunction()); } NODE_MODULE(test, init) } 

它编译,但是当我跑脚本,包括这个插件:

 var test = require('./build/Release/test'); console.log(test.hello()); // 'world' 

我得到了错误信息:

 node.js:199 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: Unable to load shared library /home/run/git/addontest/build/Release/test.node at Object..node (module.js:474:11) at Module.load (module.js:350:32) at Function._load (module.js:308:12) at Module.require (module.js:356:17) at require (module.js:372:17) at Object.<anonymous> (/home/run/git/addontest/octonode.js:1:74) at Module._compile (module.js:443:26) at Object..js (module.js:461:10) at Module.load (module.js:350:32) at Function._load (module.js:308:12) 

我的g ++版本是(Ubuntu / Linaro 4.5.2-8ubuntu4)4.5.2和nodejs版本是v0.7.5-pre

你能检查你是否有任何问题编译和运行此代码?

它看起来并不喜欢你没有为你的静态variables分配空间的事实。

添加这个修复它给我。

 template <class S> std::vector< S > FooString<S>::vec_strings;