简体   繁体   English

错误 LNK2001:使用 Node-Gyp 的 C++ 中的外部符号无法解析

[英]error LNK2001: unresolved external symbol in C++ with Node-Gyp

Hello I am new to C++ and have been building an addon for one of my programs using node-gyp.您好,我是 C++ 的新手,并且一直在使用 node-gyp 为我的一个程序构建一个插件。 However, when I try to build the addon I run into this error:但是,当我尝试构建插件时,我遇到了这个错误:

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)

I believe it has to do with reference errors accessing info from a JSON.我相信这与从 JSON 访问信息的参考错误有关。 This is what my code looks like:这就是我的代码的样子:

#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)
}

I am not sure on what to do as I've never seen an error like this before and there is little documentation online on how to deal with it.我不确定该怎么做,因为我以前从未见过这样的错误,而且网上几乎没有关于如何处理它的文档。 Thank you!谢谢!

This is my binding.gyp:这是我的 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",


            ],
        },
    ]
}

You need to add the jsoncpp library to the linking phase in binding.gyp :您需要将jsoncpp库添加到binding.gyp的链接阶段:

"libraries": [
  "-L.", "-ljsoncpp"
],
  • -L<libdir> - The directory to look for the library in. Replace . -L<libdir> - 在其中查找库的目录。替换. with the actual path if it's not in your current directory.如果它不在您的当前目录中,则使用实际路径。
  • -l<library> - The actual library to link with. -l<library> - 要链接的实际库。 You may want to specify the full static library name jsoncpp.lib instead to avoid problems if jsoncpp.dll exists too.如果jsoncpp.dll也存在,您可能需要指定完整的 static 库名称jsoncpp.lib以避免出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM