Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static linking not working for node module

I am developing a native node module for AWS lambda. This node module needed json-c

As per AWS lambda guidelines the node module should not have dynamic dependencies. So tried linking the static version of json-c library. But I am getting compile time errors .

As a node module is just a shared library, I wrote a sample C application(with main renamed) to simulate the node module compilation and these are the results:

g++      -shared -pthread -rdynamic -m64  -Wl,-soname=addon.node -o addon.node testjson.cpp  -I /usr/include/json-c/  -L  /lib/x86_64-linux-gnu/  -l:libjson-c.a
testjson.cpp: In function ‘int test()’:
testjson.cpp:6:14: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
              ^
/usr/bin/ld: /tmp/ccihB9d8.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/tmp/ccihB9d8.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

And when I tried with "--whole-archive":

g++ -shared -o libshared.so -Wl,--whole-archive -fPIC -l:/usr/lib/x86_64-linux-gnu/libjson-c.a  -Wl,--no-whole-archive testjson.cpp -I /usr/include/json-c/
testjson.cpp: In function ‘int test()’:
testjson.cpp:6:14: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  char *str = "{ \"msg-type\": [ \"0xdeadbeef\", \"irc log\" ], \
              ^
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libjson-c.a(json_c_version.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib/x86_64-linux-gnu/libjson-c.a(json_c_version.o): error adding symbols: Bad value
collect2: error: ld returned 1 exit status

What am I doing wrong?Is it not possible to statically link a library to a shared object?

like image 413
kiran Avatar asked May 24 '16 16:05

kiran


1 Answers

First you need to manually build json-c as static library.

Create Json-c Static Lib

  • wget https://s3.amazonaws.com/json-c_releases/releases/json-c-0.11.tar.gz
  • tar -xvzf json-c-0.11.tar.gz
  • cd json-c-0.11
  • autogen
  • ./configure --enable-static --with-pic --prefix=/var/task.
  • make
  • make install

After configure your binding.gyp file like this(it is applicable for building source code to npm library using node-gyp tool).

{
  "targets": [
     {
      "target_name": "testName",
      "sources": ["yourCode.c"],
      "libraries": ["/var/task/lib/libjson-c.a"]
     }
  ]
}

Its working for me.

like image 55
Abdul Manaf Avatar answered Oct 06 '22 07:10

Abdul Manaf