0

I have a project which I compile on Linux. When I run the ldd command against the executable, here's what I get:

libevent_core-2.1.so.6 => /usr/lib/x86_64-linux-gnu/libevent_core-2.1.so.6 (0x00007fca87a5e000)
libevent_pthreads-2.1.so.6 => /usr/lib/x86_64-linux-gnu/libevent_pthreads-2.1.so.6 (0x00007fca8785b000)

Now, I want to make these libraries compile as static libraries. How can I do that?

Here's my make file library:

LIBS    = -levent_core -levent_extra -levent -levent_pthreads -lsystemd
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
RookieDev
  • 43
  • 6
  • 1
    I don't believe you can without the source. shared libraries are compiled compiled with as position independent addressing `-fPIC` so without the code I think your out of luck. Unless I'm misunderstanding you. Is this your code? – jiveturkey Oct 03 '18 at 16:10
  • @jiveturkey No this is not my code. I have source code files but i just need to make some library as static instead of dynamic. i have mentioned libraries which i want to convert to static and i have make file which i need to change. – RookieDev Oct 03 '18 at 17:59
  • You need to use `ar` to create the library after compiling. https://stackoverflow.com/questions/5947067/how-to-create-a-static-library-with-g – jiveturkey Oct 03 '18 at 18:14
  • @jiveturkey i already have .a file in dev package of libevent, i just need to make it as static. – RookieDev Oct 03 '18 at 21:05

1 Answers1

3

There is no makefile magic that turns shared libraries into static libraries. You need to install the static versions of the libraries on your system and then, in your makefile, specify that the static versions of the libraries are to be linked.

It appears that the static libraries you would need to install are:

libevent_core.a
libevent_extra.a
libevent.a
libevent_pthreads.a
libsystemd.a

Having installed those libraries, you would modify your makefile to link them statically by changing:

LIBS    = -levent_core -levent_extra -levent -levent_pthreads -lsystemd

to:

LIBS    = -Wl,-Bstatic -levent_core -levent_extra -levent -levent_pthreads -lsystemd -Wl,-Bdynamic

However, you cannot do exactly that, because there is no static version of libsystemd. Here's why.

There are static versions of the other libraries in your list. You can install them by installing the libevent development package (probably package libevent-dev or libevent-devel, depending on your linux distro). Then you can link those ones statically with:

LIBS    = -Wl,-Bstatic -levent_core -levent_extra -levent -levent_pthreads -Wl,-Bdynamic -lsystemd

Note that there are no spaces within -Wl,-Bstatic or -Wl,-Bdynamic. GCC options of the form -Wl,... mean that GCC should pass the options ... through to its invocation of the linker.

Here is the documentation of the linker options

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
  • Thanks for help. You are right, i dont need systemd lib as static. I tried to change my make file as you suggested but i am getting following error when i compile through make -f Makefile : /usr/bin/ld: cannnot find: No such file or directory. Collect2 : error: ld returned 1 exist status. – RookieDev Oct 03 '18 at 20:54
  • @RookieDev `/usr/bin/ld: cannnot find:` is not the complete error message. You have left out the library that it cannot find. – Mike Kinghan Oct 03 '18 at 21:27
  • gcc -o aftpd aftpd.o cmd_parse.o fs_malloc.o ftpcmd.o ftpdata.o string.o fs_vector.o ./linux/loglinux.o ./linux/systemd.o -Wl, -Bstatic -levent_core -levent_extra -levent -levent_pthreads -Wl, -Bdynamic -lsystemd /usr/bin/ld: cannot find : No such file or directory /usr/bin/ld: cannot find : No such file or directory collect2: error: ld returned 1 exit status Makefile:15: recipe for target 'aftpd' failed make: *** [aftpd] Error 1 @Mike Kinghan This is my full error message – RookieDev Oct 04 '18 at 19:57
  • 1
    @RookieDev `-Wl, -Bstatic` is wrong. `-Wl,-Bstatic` is right. There is no space in the middle. That is the cause of `cannot find : No such file or directory`. The same with `-Wl, -Bdynamic`. – Mike Kinghan Oct 04 '18 at 21:29