How to use static library(.a file) instead of a group of object files(.o) to create a shared library

Kanishka Khandelwal

I have a shared library and I use it to create an executable binary. I can only control the build process of the library and the executable binary and not the source files involved. As expected, the source files in executable binary refer to a lot of functions from the library.

Currently the shared library is built using the objectfiles(.o) directly.

g++ -shared ${OBJECT_FILES} -o ${SHARED_LIBRARY}

I want to publish the object files too by grouping them in a static library (.a file or achive).

To save on space, I delete all the .o files on creation of the archive file. So now, the build command is

g++ -shared ${ACRCHIVE_FILE} -o ${SHARED_LIBRARY}

The library builds fine.
But when I try to build the executable binary by linking to this shared library, the symbols refereed to by the binary are not defined and it fails to link. (undefined reference to Context::Get())

By my understanding it should not matter if we create the shared library using .o files directly or an archive consisting of all the .o files, but evidently either it is not possible or I may be missing something.

Basile Starynkevitch

Shared libraries libfoo.so should contain PIC code and static libraries libfoo.a contain ordinary non PIC code. So you cannot create a shared library from a static one.

Shared libraries want Position Independent Code because their segment(s) is mmap(2)-ed at nearly arbitrary and variable addresses - without MAP_FIXED ... See also ASLR.

You could in principle build a static library from PIC object files, but nobody does that in practice.

You might (if you really insist) make an ELF shared object made of non-PIC code, but the result would have very bad performance; the dynamic linker would have a lot of relocations, so most segments would be unshared and the dynamic linking would be very slow.

To compile foo.cc for a shared library into a PIC object file foo.pic.o :

g++ -Wall -c -O foo.cc  -fPIC -o foo.pic.o

To compile it for a static library int an ordinary non-PIC object file foo.o :

g++ -Wall -c -O foo.cc -o foo.o 

To make a shared library of foo.pic.o and bar.pic.o into libfoobar.so linking in some libdep.so shared library:

g++ -shared foo.pic.o bar.pic.o -ldep -o libfoobar.so

You often want to add more linking options when making a shared library like above, e.g. -Wl,-rpath,... or -Wl,-soname,....

BTW, it is not possible to link an archive libfoobar.a (even if it is made of PIC files) into a libfoobar.so because no name is undefined and requires linking some object files from libfoobar.a (maybe you could try to undefine some symbol symb with -u symb but I don't recommend doing that).

To make a static library of foo.o and bar.o into libfoobar.a :

ar cv libfoobar.a foo.o bar.o

Notice that ranlib (creating an index of the archive) is no more necessary since GNU ar does its job.

Read also ld.so(8), ldd(1), ld(1), ar(1), objdump(1), readelf(1), dlopen(3) (often, you need to link the main program with -rdynamic if it loads dlopen-ed plugins at runtime, to enable the plugin to find some symbols in the main program).

NB: I would just build a shared library and not care about static linking at all. Notice that on some systems, PIC has a slight cost (slightly bigger and/or slower code). AFAIK, PIC overhead is less costly on x86-64 than on 32 bits Linux x86 code. On some architectures and ABIs PIC may have negligible overhead (or perhaps even be more efficient than non position independent code).

References: Drepper's paper: How to Write Shared Libraries & Levine's book: Linkers and Loaders & Program Library HowTo

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to check if a shared library is installed?

분류에서Dev

Deleting a file object from a library

분류에서Dev

is dlopen use inside a static library in iOS allowed

분류에서Dev

Use different library instead of jQuery Mobile for <select>

분류에서Dev

Makefile: How write a rule that is only executed when a certain shared object library doesn't exist?

분류에서Dev

how to get object with retrofit library?

분류에서Dev

How to use functions from a C shared library generated by MATLAB Compiler using Python

분류에서Dev

How to use ImageLoader of Volley Library?

분류에서Dev

How to use a custom library in Maven?

분류에서Dev

Installing and using a shared library

분류에서Dev

Linking dependency to static library?

분류에서Dev

Linking a static library

분류에서Dev

Minimizing dependencies for Linux shared library

분류에서Dev

Shared library minor version management

분류에서Dev

How to add and use a jar library in IntelliJ IDEA?

분류에서Dev

How to use CMake to include a library, then include its headers using angled brackets in source file?

분류에서Dev

How to include library files while creating jar files without copying library separately

분류에서Dev

How to call a C compiler through library instead of via system call?

분류에서Dev

JAXB - adapter for a library object

분류에서Dev

SQL Server CLR Library Stored Procedure static object instantiated more than once

분류에서Dev

Rcpp: error occured building shared library

분류에서Dev

Link shared library with each other shared libraries c++

분류에서Dev

How to use command lines to install GD library for Drupal on Linux?

분류에서Dev

How can I use Xamarin.Forms in a NUnit Library project?

분류에서Dev

How to find/use a library on system without using Autoconf or Cmake?

분류에서Dev

How do you use Python's xml library to parse the character &?

분류에서Dev

How do you use Python's xml library to parse the character &?

분류에서Dev

How to use Autocomplete with jqGrid library Lib.Web.MVC

분류에서Dev

How to fix the model to correctly use the MVC Foolproof library?

Related 관련 기사

  1. 1

    How to check if a shared library is installed?

  2. 2

    Deleting a file object from a library

  3. 3

    is dlopen use inside a static library in iOS allowed

  4. 4

    Use different library instead of jQuery Mobile for <select>

  5. 5

    Makefile: How write a rule that is only executed when a certain shared object library doesn't exist?

  6. 6

    how to get object with retrofit library?

  7. 7

    How to use functions from a C shared library generated by MATLAB Compiler using Python

  8. 8

    How to use ImageLoader of Volley Library?

  9. 9

    How to use a custom library in Maven?

  10. 10

    Installing and using a shared library

  11. 11

    Linking dependency to static library?

  12. 12

    Linking a static library

  13. 13

    Minimizing dependencies for Linux shared library

  14. 14

    Shared library minor version management

  15. 15

    How to add and use a jar library in IntelliJ IDEA?

  16. 16

    How to use CMake to include a library, then include its headers using angled brackets in source file?

  17. 17

    How to include library files while creating jar files without copying library separately

  18. 18

    How to call a C compiler through library instead of via system call?

  19. 19

    JAXB - adapter for a library object

  20. 20

    SQL Server CLR Library Stored Procedure static object instantiated more than once

  21. 21

    Rcpp: error occured building shared library

  22. 22

    Link shared library with each other shared libraries c++

  23. 23

    How to use command lines to install GD library for Drupal on Linux?

  24. 24

    How can I use Xamarin.Forms in a NUnit Library project?

  25. 25

    How to find/use a library on system without using Autoconf or Cmake?

  26. 26

    How do you use Python's xml library to parse the character &?

  27. 27

    How do you use Python's xml library to parse the character &?

  28. 28

    How to use Autocomplete with jqGrid library Lib.Web.MVC

  29. 29

    How to fix the model to correctly use the MVC Foolproof library?

뜨겁다태그

보관