【问题标题】:Undefined symbol when Calling function in C++ Random Char added添加了在 C++ Random Char 中调用函数时未定义的符号
【发布时间】:2016-08-09 16:15:12
【问题描述】:

在 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-gyp configure:有效
  • node-gyp build:有效
  • LD_LIBRARY_PATH=libs/ node index.js: 不行

我收到以下错误

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

当函数被调用时,它会被添加随机字符。我假设这是随机数据,是内存中的一些噪音。看起来好像调用函数的刹车尺寸比它应该的大。

我没有混合 C++ 和 C 的经验,我很想听听正在发生的事情的解释。

技术规格:

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

【问题讨论】:

标签: c++ node.js v8


【解决方案1】:

调用该函数时,它会在前面加上随机字符

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

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

您可能采取的行动:

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

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

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

【讨论】:

  • 那你在理解方面的帮助。关于您的更新,我在 .h 文件中有。所以在这方面我已经涵盖了。
  • 其实真正的问题出在Extern C,我被.h文件#define VE_EXPORT extern "C" __declspec(dllexport)中的这一行误导了,我以为就是这样。一旦我用extern 包装了声明,一切都奏效了 - 非常感谢:)
猜你喜欢
  • 2011-03-16
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
相关资源
最近更新 更多