我正在使用Visual Studio中的node.js模块,并具有以下文件结构:
load.h
#include <string>
#include <v8.h>
void property_guard(v8::Local<v8::Object> obj, v8::Local<v8::String> name, const std::string path);
load.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也可以解决问题。
在这种情况下,将声明移动到其他文件的正确方法是什么?
我的环境: