[英]error LNK2001: unresolved external symbol in C++ with Node-Gyp
您好,我是 C++ 的新手,並且一直在使用 node-gyp 為我的一個程序構建一個插件。 但是,當我嘗試構建插件時,我遇到了這個錯誤:
watercpp.obj : error LNK2001: unresolved external symbol "class std::basic_istr
eam<char,struct std::char_traits<char> > & __cdecl Json::operator>>(class std::
basic_istream<char,struct std::char_traits<char> > &,class Json::Value &)" (??5
Json@@YAAEAV?$basic_istream@DU?$char_traits@D@std@@@std@@AEAV12@AEAVValue@0@@Z)
我相信這與從 JSON 訪問信息的參考錯誤有關。 這就是我的代碼的樣子:
#include "node.h"
#include "node_buffer.h"
#include "v8.h"
#include <value.h>
#include <fstream>
#include <iostream>
#include <string>
#include <json/json.h>
#include <vector>
using namespace v8;
using namespace std;
namespace water{
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Number;
using v8::Value;
using v8::Array;
void water(const FunctionCallbackInfo<Value> &args)
{
Json::Value waterjson;
std::ifstream people_file("waterjson.json", std::ifstream::binary);
people_file >> waterjson;
Local<Array> a;
Local<Array> b;
if(args[0]->IsArray())
{
a = args[0].As<Array>();
b = args[1].As<Array>();
}
int total=0;
for(std::size_t i=0; i<a->Length(); i++)
{
v8::Isolate* isolate = args.GetIsolate();
v8::String::Utf8Value atr(isolate, a->Get(i));
std::string cppStr(*atr);
int c = waterjson[cppStr]["content"].asInt();
int s = waterjson[cppStr]["serving"].asInt();
int m = s/100;
int amount = c*m;
v8::String::Utf8Value btr(isolate, b->Get(i));
std::string cppNum(*btr);
int num = stoi(cppNum);
total+=(amount*num);
}
args.GetReturnValue().Set(total);
}
void Initialize(Local<Object> exports)
{
NODE_SET_METHOD(exports, "water", water);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
}
我不確定該怎么做,因為我以前從未見過這樣的錯誤,而且網上幾乎沒有關於如何處理它的文檔。 謝謝!
這是我的 binding.gyp:
{
"targets":[
{
"target_name": "water",
"sources": ["watercpp.cpp"],
"include_dirs": [
"./vcpkg/installed/x86-windows/include",
"./vcpkg/buildtrees/jsoncpp/src/1.9.2-d01d7f5c9b.clean/include/json",
],
"library_dirs":[
"./vcpkg/buildtrees/jsoncpp/x86-windows-dbg/src/lib_json",
"./vcpkg/installed/x86-windows/lib", "./vcpkg/packages/jsoncpp_x86-windows/lib",
],
"libraries":[
"-ljsoncpp.lib", "<(module_root_dir)/vcpkg/buildtrees/jsoncpp/x86-windows-dbg/src/lib_json",
"<(module_root_dir)/vcpkg/installed/x86-windows/lib", "<(module_root_dir)/vcpkg/packages/jsoncpp_x86-windows/lib",
],
},
]
}
您需要將jsoncpp
庫添加到binding.gyp
的鏈接階段:
"libraries": [
"-L.", "-ljsoncpp"
],
-L<libdir>
- 在其中查找庫的目錄。替換.
如果它不在您的當前目錄中,則使用實際路徑。-l<library>
- 要鏈接的實際庫。 如果jsoncpp.dll
也存在,您可能需要指定完整的 static 庫名稱jsoncpp.lib
以避免出現問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.