title: Getting "Symbol Lookup Error" when calling C library from C++ (Node.js Addon)
body: <p>I am working on a Node.js addon that needs to wrap the objects from a <a href="https://github.com/Yona-Appletree/LEDscape" rel="noreferrer">C library</a> in C++ so they can be accessed from client-side JavaScript (written in CoffeeScript).</p>
  
  <p>The C++ module compiles, but when I try to run it through Node.js JavaScript, the C library fails to be called with a <code>symbol lookup error</code> that I'm having problems debugging.</p>
  
  <p>The error is below:<br>
  <code>node: symbol lookup error: /var/lib/cloud9/ledscape-wrapper/wrapper/build/Release/wrapper.node: undefined symbol: ledscape_init
  </code></p>
  
  <p><code>wrapper.node</code> is the compiled package, <code>ledscape_init</code> is the function in the library I am trying to call.</p>
  
  <p>I've tried to trace the code and find the relevant snippets across multiple files.  I've dropped lines I've deemed extraneous.</p>
  
  <pre class="lang-coffeescript prettyprint-override"><code># "AllFade.coffee"
  @ledscape = require "./ledscape.js"
  @frames[1] = @ledscape.LedscapeInit()
  # "Ledscape.coffee"
  wrapper = require "./build/Release/wrapper"
  module.exports = wrapper
  </code></pre>
  
  <p>wrapper.cc</p>
  
  <pre><code>extern "C" {
  #include &lt;ledscape.h&gt;
  }
  #include &lt;node.h&gt;
  #include &lt;v8.h&gt;
  #include &lt;node_object_wrap.h&gt;
  #include "LedscapeWrapper.h"
  
  Handle&lt;Value&gt; LedscapeInit(const Arguments&amp; args) {
      HandleScope scope;
      return scope.Close(LedscapeWrapper::NewInstance(args));
  }
  
  void InitAll(Handle&lt;Object&gt; exports, Handle&lt;Object&gt; module) {
      LedscapeWrapper::Init(module);
      NODE_SET_METHOD(exports, "LedscapeInit", LedscapeInit);
  }
  
  NODE_MODULE(wrapper, InitAll)
  </code></pre>
  
  <p>LedscapeWrapper.h</p>
  
  <pre><code>extern "C" {
  #include &lt;ledscape.h&gt;
  }
  #include &lt;node.h&gt;
  #include &lt;node_object_wrap.h&gt;
  using namespace v8;
  
  class LedscapeWrapper : public node::ObjectWrap {
      public:
          static void Init(v8::Handle&lt;v8::Object&gt; exports);
          static Handle&lt;Value&gt; NewInstance(const Arguments&amp; args);
          inline ledscape_t* value() const { return value_; }
      private:
          explicit LedscapeWrapper(ledscape_t* value = ledscape_init(1));
          ~LedscapeWrapper();
          static Handle&lt;Value&gt; New(const Arguments&amp; args);
          static v8::Persistent&lt;v8::Function&gt; constructor;
          ledscape_t* value_;
  };
  </code></pre>
  
  <p>LedscapeWrapper.cpp</p>
  
  <pre><code>extern "C" {
  #include &lt;ledscape.h&gt;
  }
  #include &lt;node.h&gt;
  #include "LedscapeWrapper.h"
  using namespace v8;
  
  void LedscapeWrapper::Init(Handle&lt;Object&gt; exports) {
      Local&lt;FunctionTemplate&gt; tpl = FunctionTemplate::New(New);
      tpl-&gt;SetClassName(String::NewSymbol("LedscapeWrapper"));
      tpl-&gt;InstanceTemplate()-&gt;SetInternalFieldCount(1);
      constructor = Persistent&lt;Function&gt;::New(tpl-&gt;GetFunction());
      exports-&gt;Set(String::NewSymbol("LedscapeWrapper"), constructor);
  }
  
  Handle&lt;Value&gt; LedscapeWrapper::New(const Arguments&amp; args) {
      HandleScope scope;
      if(args.IsConstructCall()) {
          ledscape_t* ledscape = ledscape_init(args[0]-&gt;NumberValue());
          LedscapeWrapper* obj = new LedscapeWrapper(ledscape);
          obj-&gt;Wrap(args.This());
          return args.This();
      }
      else {
          const int argc = 1;
          Local&lt;Value&gt; argv[argc] = { args[0] };
          return scope.Close(constructor-&gt;NewInstance(argc, argv));
      }
  }
  
  Handle&lt;Value&gt; LedscapeWrapper::NewInstance(const Arguments&amp; args) {
      HandleScope scope;
      const int argc = 1;
      Handle&lt;Value&gt; argv[argc] = { args[0] };
      Local&lt;Object&gt; instance = constructor-&gt;NewInstance(argc, argv);
      return scope.Close(instance);
  }
  </code></pre>
  
  <p>binding.gyp</p>
  
  <pre><code>{
    "targets": [{
      "target_name": "wrapper",
      "sources": ["wrapper.cc","LedscapeWrapper.cpp","LedscapeFrameWrapper.cpp"],
      'include_dirs': ['/opt/ledscape/'],
      'link_settings': {  'library_dirs': ['/opt/ledscape']  },
    }],
  }
  </code></pre>
  
  <p>I think the problem is in one of the calls to <code>ledscape_init()</code> inside LedscapeWrapper.cpp, and that it is unable to find the library (<a href="https://github.com/Yona-Appletree/LEDscape/blob/master/ledscape.h" rel="noreferrer">ledscape.h</a>), but I'm not primarily a C/C++ developer.</p>
  
  <p>I tried to look into the <code>nm</code> tool, either from GNU or Node, but it refused to examine the <code>.node</code> file, and I wasn't finding any usage pointers online.</p>