I have this simple code of node 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)
}
it compiles but when I’m runing script which including this addon:
var test = require('./build/Release/test');
console.log(test.hello()); // 'world'
I have got the error message:
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)
My g++ version is (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 and nodejs version is v0.7.5-pre
could you check if you have any problems to compile and run this code?
It looks like it’s not loving the fact that you haven’t allocated space for your static variable anywhere.
Adding this fixes it for me.