我正在Visual Studio中开发node.js模块,具有以下文件结构:
负载.h
#include <string>
#include <v8.h>
void property_guard(v8::Local<v8::Object> obj, v8::Local<v8::String> name, const std::string path);
加载.cpp
#include "load.h"
void property_guard(v8::Local<v8::Object> obj, v8::Local<v8::String> name, const std::string path) {
if (!obj->Has(name)) {
throw path + " does not exist";
}
}
main.cpp
#include <string>
#include <node.h>
#include <v8.h>
#include "load.h"
void integer_type_guard(v8::Local<v8::Value> obj, const std::string path) {
if (!obj->IsInt32()) {
throw path + " is not an integer";
}
}
int get_int_property(v8::Local<v8::Object> obj, v8::Local<v8::String> name, const std::string path) {
property_guard(obj, name, path);
v8::Local<v8::Value> value = obj->Get(name);
integer_type_guard(value, path);
return value->Int32Value();
}
我希望结构是正确的,但当我用node-gyp:编译项目时,我在Visual Studio中遇到了一个类似的错误:
error LNK2005: "public: class v8::Object * __cdecl v8::Handle<class v8::Object>::operator->(void)const " (??C?$Handle@VObject@v8@@@v8@@QEBAPEAVObject@1@XZ) already defined in node.lib(node.exe) C:...load.obj
error LNK1169: one or more multiply defined symbols found C:...Test1.dll
从main.cpp中删除node.h包括删除错误(我需要它来注册模块)。将property_guard方法移到main.cpp也解决了这个问题。
在这种情况下,将声明移动到其他文件的正确方法是什么?
我的环境:
- Visual Studio 2012
- node.js 0.10.12 x64
非常感谢node.js邮件列表中的Bert Belder提供的解决方案。在.cpp文件中包含任何其他内容之前,我需要添加#include。