简体   繁体   中英

How to use static library in linux executable

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM