0

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>&)
        //
        args.GetReturnValue().Set(str);
    }


    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
David Gatti
  • 3,576
  • 3
  • 33
  • 64
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Miles Budnek Aug 08 '16 at 14:47

1 Answers1

2

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

UPD.
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(...);
}
Teivaz
  • 5,462
  • 4
  • 37
  • 75
  • Then you for the help in understanding. About your update, I have that in the .h file. So in that regard I'm covered. – David Gatti Aug 08 '16 at 16:21
  • Actually, the true issue was in the Extern C, I was misled by this line in the .h file `#define VE_EXPORT extern "C" __declspec(dllexport)`, and I thought that was it. once I wrapped the declaration with `extern` it all worked - so much thank you :) – David Gatti Aug 09 '16 at 08:21