微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误LNK2001:带有Node-Gyp的C ++中未解析的外部符号

如何解决错误LNK2001:带有Node-Gyp的C ++中未解析的外部符号

您好,我是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.lib,以避免在jsoncpp.dll也存在的情况下出现问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。