title: Link Node.js C++ add-on with static library
body: <p>I have a C++ project that is intended to perform some calculations. The C++ code will sit on a server that as back end to a browser based GUI. Node.js seems to be suitable for this job. I've gone through the tutorials and learnt to build C++ code to be used as a nodejs module. </p>
  
  <p>For the sake of simplicity, I'd like to compile the C++ code as a static library. I could then write a C++ class which references this library which can then be used in the nodejs environment. The point of this is so I don't have to use node-gyp build to build my entire C++ project. This way I can further develop the C++ code without worrying too much about the front end. </p>
  
  <p>To achieve this, I have done the following:</p>
  
  <ol>
  <li><p>Built a simple C++ library as follows. Building this in Visual Studio 2013 to get the .lib file.</p>
  
  <pre><code>//MyClass.h
  #pragma once
  class MyClass
  {
  public:
      double Multiply(double a, double b)
      {
          return a*b;
      }
      MyClass();
      ~MyClass();
  };
  </code></pre></li>
  <li><p>Created the C++ object to be used as a nodejs module like so:</p>
  
  <pre><code>// myobject.h
  #ifndef MYOBJECT_H
  #define MYOBJECT_H
  
  #include &lt;node.h&gt;
  #include &lt;node_object_wrap.h&gt;
  #include "MyLib\MyClass.h"
  
  namespace demo {
  
      class MyObject : public node::ObjectWrap {
      public:
          static void Init(v8::Local&lt;v8::Object&gt; exports);
          static MyClass* mycalcobj;
  
      private:
          explicit MyObject(double value = 0);
          ~MyObject();
  
          static void New(const v8::FunctionCallbackInfo&lt;v8::Value&gt;&amp; args);
          static void PlusOne(const v8::FunctionCallbackInfo&lt;v8::Value&gt;&amp; args);
          static v8::Persistent&lt;v8::Function&gt; constructor;
          double value_;
  
  
      };
  
  }  // namespace demo
  
  #endif
  </code></pre>
  
  <p>and the cpp file</p>
  
  <pre><code>    // myobject.cpp
      #include "myobject.h"
  
      //#include "worker.h"
      namespace demo {
  
          using v8::Function;
          using v8::FunctionCallbackInfo;
          using v8::FunctionTemplate;
          using v8::Isolate;
          using v8::Local;                 
          using v8::Number;
          using v8::Object;
          using v8::Persistent;
          using v8::String;
          using v8::Value;
  
          Persistent&lt;Function&gt; MyObject::constructor;
  
          MyObject::MyObject(double value) : value_(value) {
          }
  
          MyObject::~MyObject() {
          }
  
          void MyObject::Init(Local&lt;Object&gt; exports) {
              Isolate* isolate = exports-&gt;GetIsolate();
  
              // Prepare constructor template
              Local&lt;FunctionTemplate&gt; tpl = FunctionTemplate::New(isolate, New);
              tpl-&gt;SetClassName(String::NewFromUtf8(isolate, "MyObject"));
              tpl-&gt;InstanceTemplate()-&gt;SetInternalFieldCount(1);
  
              // Prototype
              NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
  
              constructor.Reset(isolate, tpl-&gt;GetFunction());
              exports-&gt;Set(String::NewFromUtf8(isolate, "MyObject"),
                  tpl-&gt;GetFunction());
          }
  
          void MyObject::New(const FunctionCallbackInfo&lt;Value&gt;&amp; args) {
              Isolate* isolate = args.GetIsolate();
  
              if (args.IsConstructCall()) {
                  // Invoked as constructor: `new MyObject(...)`
                  double value = args[0]-&gt;IsUndefined() ? 0 : args[0]-&gt;NumberValue();
                  MyObject* obj = new MyObject(value);
                  obj-&gt;Wrap(args.This());
                  args.GetReturnValue().Set(args.This());
              }
              else {
                  // Invoked as plain function `MyObject(...)`, turn into construct call.
                  const int argc = 1;
                  Local&lt;Value&gt; argv[argc] = { args[0] };
                  Local&lt;Function&gt; cons = Local&lt;Function&gt;::New(isolate, constructor);
                  args.GetReturnValue().Set(cons-&gt;NewInstance(argc, argv));
              }
          }
  
  
          //define functions here
          void MyObject::PlusOne(const FunctionCallbackInfo&lt;Value&gt;&amp; args) {
              Isolate* isolate = args.GetIsolate();
              MyObject* obj = ObjectWrap::Unwrap&lt;MyObject&gt;(args.Holder());
              double x = obj-&gt;value_;
              mycalcobj = new MyClass();
  
              x=mycalcobj-&gt;Multiply(2, x);
  
              obj-&gt;value_= x;
  
              args.GetReturnValue().Set(Number::New(isolate, obj-&gt;value_));
          }
  
      }  // namespace demo
  </code></pre></li>
  <li><p>Create the main cpp file to initialise</p>
  
  <pre><code>    #include &lt;node.h&gt;
      #include "myobject.h"
  
      namespace demo {
  
          using v8::Local;
          using v8::Object;
  
          void InitAll(Local&lt;Object&gt; exports) {
              MyObject::Init(exports);
          }
  
          NODE_MODULE(addon, InitAll)
  
      }  // namespace demo
  </code></pre></li>
  <li><p>The binding.gyp file is defined as:</p>
  
  <pre><code>    {
        "targets": [
          {
            "target_name": "cpphello",
            "sources": [ "cpphello.cpp", "myobject.cpp"],
            "libraries": [ "D:/East101/Adri/javascript/socketio/cpplib/build/x64/Debug/MyLib"]
          }
        ]
      }
  </code></pre></li>
  <li><p>Build the project using node-gyp configure build</p></li>
  </ol>
  
  <p>I get the following messages: </p>
  
  <pre><code>    MyLib.lib(MyClass.obj) : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specification [D:\East101\Adri\javascript\socketio\cpplib\build\cpphello.vcxproj]
  
  
      LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs;use /NODEFAULTLIB:library [D:\East101\Adri\javascript\socketio\cpplib\build\cpphello.vcxproj]
  
      myobject.obj : error LNK2001: unresolved external symbol "public: static class MyClass * demo::MyObject::mycalcobj" (?mycalcobj@MyObject@demo@@2PEAVMyClass@@EA) [D:\East101\Adri\javascript\socketio\cpplib\build\cpphello.vcxproj] D:\East101\Adri\javascript\socketio\cpplib\build\Release\cpphello.node : fatal error LNK1120: 1 unresolved externals [D:East101\Adri\javascript\socketio\cpplib\build\cpphello.vcxproj]
  </code></pre>
  
  <p>I need some assistance to either fix this error and make it work as I envision it or let me know if there's a better way. I'm quite new to C++ and coding in general so I may just be approaching this wrong.</p>