David Gatti David Gatti - 1 year ago 173
C++ Question

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

In NodeJS I'm building an interface to a shared object in C. I have the following code:

#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>&)

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

NODE_MODULE(addon, Init)

  • node-gyp configure
    : works

  • node-gyp build
    : works

  • LD_LIBRARY_PATH=libs/ node index.js
    : doesn't work

I get the following error:

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

The when the function is called it gets prepended and appended with random characters. I'm assuming this is random data are some noise from memory. It seams as if the size brakes to call the function is bigger then it should.

I'm not that experienced with mixing C++ and C, I would love to get an explanation on what is happening hear.

Tech specs:

  • GCC Version: gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

  • NodeJS Version: v6.2.0

Answer Source

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

It is called name mangling that happens in C++.

The actual error here is that compiled module can not link to function device_get_version().

Your possible actions:

  • add the implementation of device_get_version to your module
  • properly link this function
  • simply remove that line and the error will disappear

device_get_version may actually be a C function which is treated as a C++ function (you can tell it by mangled name it has). Make sure your function declared as

extern "C" {
    void device_get_version(...);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download