代码之家  ›  专栏  ›  技术社区  ›  Denis Steinman

engine.node:未定义的符号:_ZTV6Config

  •  0
  • Denis Steinman  · 技术社区  · 4 年前

    Node.JS节点 N-Api加载项,但它与日志一起崩溃:

    internal/modules/cjs/loader.js:718
      return process.dlopen(module, path.toNamespacedPath(filename));
                     ^
    
    Error: /home/d/Projects/engine/build/Release/engine.node: undefined symbol: _ZTV6Config
        at Object.Module._extensions..node (internal/modules/cjs/loader.js:718:18)
        at Module.load (internal/modules/cjs/loader.js:599:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
        at Function.Module._load (internal/modules/cjs/loader.js:530:3)
        at Module.require (internal/modules/cjs/loader.js:637:17)
        at require (internal/modules/cjs/helpers.js:22:18)
        at Object.<anonymous> (/home/d/Projects/engine/index.js:1:78)
        at Module._compile (internal/modules/cjs/loader.js:689:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
        at Module.load (internal/modules/cjs/loader.js:599:32)
    


    包装纸.h :

    #include <napi.h>
    #include "types.hpp"
    
    using namespace std;
    
    class Config: public Napi::ObjectWrap<Config> {
        config_t _cfg;
    public:
        explicit Config(const Napi::CallbackInfo &info);
        ~Config();
        static Napi::FunctionReference constructor;
        static Napi::Object Init(Napi::Env &env, Napi::Object &exports);
    
        config_t Get();
    private:
        Napi::Value GetTimeAccuracy(const Napi::CallbackInfo &info);
        void SetTimeAccuracy(const Napi::CallbackInfo &info, const Napi::Value &value);
    };
    

    包装.cc

    Config::Config(const Napi::CallbackInfo &info): Napi::ObjectWrap<Config>(info) {
    
    }
    
    Napi::Object Config::Init(Napi::Env &env, Napi::Object &exports) {
        // This method is used to hook the accessor and method callbacks
        Napi::Function func = DefineClass(env, "Config", {
            InstanceAccessor("timeAccuracy", &Config::GetTimeAccuracy, &Config::SetTimeAccuracy)
        });
        // Create a peristent reference to the class constructor. This will allow
        // a function called on a class prototype and a function
        // called on instance of a class to be distinguished from each other.
        constructor = Napi::Persistent(func);
        // Call the SuppressDestruct() method on the static data prevent the calling
        // to this destructor to reset the reference when the environment is no longer
        // available.
        constructor.SuppressDestruct();
        exports.Set("Config", func);
        return exports;
    }
    
    Napi::FunctionReference Config::constructor;
    
    config_t Config::Get() {
        return _cfg;
    }
    
    Napi::Value Config::GetTimeAccuracy(const Napi::CallbackInfo &info) {
        Napi::Env env = info.Env();
        return Napi::Number::New(env, _cfg.time_accuracy);
    }
    
    void Config::SetTimeAccuracy(const Napi::CallbackInfo &info, const Napi::Value &value) {
        _cfg.time_accuracy = value.As<Napi::Number>().FloatValue();
    }
    

    发动机 :

    #include <napi.h>
    #include "wrapper.h"
    
    Napi::Object init(Napi::Env env, Napi::Object exports) {
        Config::Init(env, exports);
        return exports;
    }
    
    NODE_API_MODULE(engine, init);
    

    装订.gyp

    {
        "targets": [{
            "cflags_cc": ["-std=c++17"],
            "include_dirs": [
                "<!@(node -p \"require('node-addon-api').include\")"
            ],
            "target_name": "engine",
            "sources": [
                "wrapper.cc",
                "engine.cc",
                "types.cpp",
            ],
            "defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
        }]
    }
    

    当我重建项目时,它没有任何错误。在我写一个简单的 你好,世界! 我刚刚注册了一个打印文本的函数,一切正常。但现在,当我尝试将模块导入为 const engine = require('./build/Release/engine.node');
    当我得到模块寄存器但Node.JS找不到 Config .

    1 回复  |  直到 4 年前
        1
  •  1
  •   Alan Birtles    4 年前

    _ZTV6Config mangled name vtable for Config .

    令人困惑的是,这实际上是由于 Config .

    编译器在找到析构函数定义的同一翻译单元中定义vtable。