c++ - 添加 C++ 随机字符中调用函数时的 undefined symbol

标签 c++ node.js v8

在 NodeJS 中,我正在用 C 构建一个共享对象的接口(interface)。我有以下代码:

#include <node.h>
#include "libcustom_encryption.h"

namespace demo {

    using v8::Exception;
    using v8::FunctionCallbackInfo;
    using v8::Isolate;
    using v8::Local;
    using v8::Number;
    using v8::Object;
    using v8::String;
    using v8::Value;

    //
    //  This is the implementation of the "add" method
    //  Input arguments are passed using the
    //  const FunctionCallbackInfo<Value>& args struct
    //
    void DeviceGetVersion(const FunctionCallbackInfo<Value>& args)
    {
        char ver[10] = {0};
        unsigned int ver_size = 0;

        device_get_version(ver, ver_size);

        Isolate* isolate = args.GetIsolate();

        //
        //  1.  Save the value in to a isolate thing
        //
        Local<Value> str = String::NewFromUtf8(isolate, "Test");

        //
        //  2.  Set the return value (using the passed in
        //      FunctionCallbackInfo<Value>&)
        //
        args.GetReturnValue().Set(str);
    }


    void Init(Local<Object> exports)
    {
        NODE_SET_METHOD(exports, "devicegetversion", DeviceGetVersion);
    }

    NODE_MODULE(addon, Init)
}
  • node-gyp 配置:有效
  • node-gyp 构建:有效
  • LD_LIBRARY_PATH=libs/node index.js:不工作

我收到以下错误:

node: symbol lookup error: /long_path/build/Release/app.node: undefined symbol: _Z18device_get_versionPcS_Phj

调用该函数时,它会在前面和后面附加随机字符。我假设这是随机数据是内存中的一些噪音。它接缝就好像 brakes 调用函数的大小比它应该的要大。

我在混合使用 C++ 和 C 方面经验不足,我很乐意听到正在发生的事情的解释。

技术规范:

  • GCC 版本:gcc 版本 4.8.5 20150623(Red Hat 4.8.5-4)(GCC)
  • NodeJS 版本:v6.2.0

最佳答案

the function is called it gets prepended and appended with random characters

它被称为 name mangling这发生在 C++ 中。

这里的实际错误是编译模块无法链接到函数device_get_version()

您可能采取的行动:

  • device_get_version 的实现添加到您的模块
  • 正确链接此功能
  • 只需删除该行,错误就会消失

更新。
device_get_version 实际上可能是一个 C 函数,它被视为 C++ 函数(您可以通过它的名称来判断)。 确保你的函数声明为

extern "C" {
    void device_get_version(...);
}

关于c++ - 添加 C++ 随机字符中调用函数时的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830999/

相关文章:

node.js - 使用 Mongoose (MongoDB) 查询嵌套文档

Javascript Engine V8 快速属性访问

c++ - 使用指针数组时编程 "has stopped working"

c++ - OpenCV 3 似乎不适用于 Qt

javascript - Mongodb 查找 - 或运算符?

node.js - Cloudfoundry 上的 Restify 'invalid ELF header'

Perl:Javascript::V8 模板 - 来自 perl

c++ - v8 作用域是如何工作的?

c++ - ANTLR4和C++入门

c++ - OpenCV FileStorage 如何读取结构 vector ?