添加 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-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
调用该函数时,它会在前面加上随机字符。我假设这是随机数据是内存中的一些噪音。它接缝就好像 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++ 中。
这里的实际错误是编译后的模块无法link发挥作用device_get_version()
。
您可能采取的行动:
- 将
device_get_version
的实现添加到您的模块中
- 妥妥的link这个功能
- 只需删除该行,错误就会消失
UPD.
device_get_version
实际上可能是一个 C 函数,它被视为 C++ 函数(您可以通过它的名称来判断)。
确保您的函数声明为
extern "C" {
void device_get_version(...);
}
在 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
调用该函数时,它会在前面加上随机字符。我假设这是随机数据是内存中的一些噪音。它接缝就好像 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++ 中。
这里的实际错误是编译后的模块无法link发挥作用device_get_version()
。
您可能采取的行动:
- 将
device_get_version
的实现添加到您的模块中 - 妥妥的link这个功能
- 只需删除该行,错误就会消失
UPD.
device_get_version
实际上可能是一个 C 函数,它被视为 C++ 函数(您可以通过它的名称来判断)。
确保您的函数声明为
extern "C" {
void device_get_version(...);
}