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
4
  • 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? Commented Oct 3, 2018 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. Commented Oct 3, 2018 at 17:59
  • You need to use ar to create the library after compiling. stackoverflow.com/questions/5947067/… Commented Oct 3, 2018 at 18:14
  • @jiveturkey i already have .a file in dev package of libevent, i just need to make it as static. Commented Oct 3, 2018 at 21:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 /usr/bin/ld: cannnot find: is not the complete error message. You have left out the library that it cannot find.
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 -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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.