title: Node native module - linking a static library
body: <p>I'm building a NodeJS native module which has a dependency on another library (libboost-regex).</p>
  
  <p>If I dynamically link the library using the below binding.gyp file everything works:</p>
  
  <pre><code>{
    "targets": [
      {
        "libraries": [
            "/usr/lib/x86_64-linux-gnu/libboost_regex.so",
            "-ludev"
        ],
        "target_name": "utils",
        "sources": [ "src/native/utils.cpp" ]
      }
    ]
  }
  </code></pre>
  
  <p>However this way it works only on my machine. On another Linux machine it says this module libboost_regex.so is not found.
  And on some Linux distros this version (currently 1.58) is not even available.</p>
  
  <p>Is it possible to staticly link it, so it gets included into my output module?</p>
  
  <p>I read a post here that I only need to change the .so version to .a version which is the static library:
  <a href="https://github.com/nodejs/node-gyp/issues/328#issuecomment-24335081" rel="nofollow noreferrer">https://github.com/nodejs/node-gyp/issues/328#issuecomment-24335081</a></p>
  
  <p>However if I replace it with this:
  "/usr/lib/x86_64-linux-gnu/libboost_regex.a"</p>
  
  <p>I now get this error:</p>
  
  <pre><code>/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_regex.a(instances.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
  /usr/lib/x86_64-linux-gnu/libboost_regex.a: error adding symbols: Bad value
  collect2: error: ld returned 1 exit status
  utils.target.mk:125: recipe for target 'Release/obj.target/utils.node' failed
  make: *** [Release/obj.target/utils.node] Error 1
  </code></pre>
  
  <p>The above error also appears if I add -static to the linker options.</p>
  
  <p>Another attempt:</p>
  
  <pre><code>{
    "targets": [
      {
        "libraries": [
            "-ludev", "-lboost_regex"
        ],
        "target_name": "utils",
        "cflags!": ["-fPIC"],
        "ldflags" : [ "-Wl,-static" ],
        "sources": [ "src/native/utils.cpp" ]
      }
    ]
  }
  </code></pre>
  
  <p>Leads to a similar error:</p>
  
  <pre><code>/usr/bin/ld: Release/obj.target/utils/src/native/utils.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
  Release/obj.target/utils/src/native/utils.o: error adding symbols: Bad value
  collect2: error: ld returned 1 exit status
  utils.target.mk:123: recipe for target 'Release/obj.target/utils.node' failed
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <p>Thanks!</p>