添加 C++ 随机字符中调用函数时的未定义符号

Undefined symbol when Calling function in C++ Random Char added

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

#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: symbol lookup error: /long_path/build/Release/app.node: undefined symbol: _Z18device_get_versionPcS_Phj

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

我对混合使用 C++ 和 C 没有那么丰富的经验,我很乐意听到正在发生的事情的解释。

技术规格:

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

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

这里的实际错误是编译后的模块无法link发挥作用device_get_version()

您可能采取的行动:

  • device_get_version 的实现添加到您的模块中
  • 妥妥的link这个功能
  • 只需删除该行,错误就会消失

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

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