C / C ++ Static vs dynamic libraries example

I study static and dynamic libraries. Until now, I understand why I need a dynamic library. In case something changes, itโ€™s good to connect a new version, and all applications will be automatically updated without even noticing.

a) Great for plugins, b) several applications using the same library, and c) maintenance when you need to fix errors.

However, why would anyone use a static library? I mean, what is the advantage? Does sb have an example to better understand it? Is this the property of the product?

EDITOR: Due to the confusion in the comments. I understand what a static library is, and I also know the difference between a dynamic library. It was just outside of me why someone used a static library instead of the source itself. I think that now I am starting to understand that a static library offers the following advantages:

a) better code maintenance b) faster compilation time

+3
source share
7 answers

The static library is basically a ZIP file of object files. The only advantage it has in distributing object files is that it is one file that spans your entire library. Users can use your headers and lib to create their applications.

ZIP , , , , , ( Generation Code-Time Code Generation). , , .

. . MSV++ "" EXE-, "" EXE- . , , .

+4

, , , .

  • , (, ) (), .

  • , ( ). . , , , , .

Linux - . , . , , 1.0 OpenOffice 1,5 !

, LD_DEBUG LD_DEBUG_OUTPUT - , , , .

+7

, . , . . . , . . , .

, , , . , , . , , .

, , , , . , , dll/so .

+5

, - dll. .

.

WIKI

0

, , ( ), , , . , .

0

, , . , , , , . , , . , .

, Solaris , , /sbin. sbin . , .

0

1.) (-fpic -fPIC ), . ; :

* ,

long realfoo(long, long);
long foo(long x, long y){
    return realfoo(x,y);
}
//static
foo:
  jmp realfoo #
//shared (-fpic code)
foo:
  pushl %ebx #
  call __x86.get_pc_thunk.bx #
  addl $_GLOBAL_OFFSET_TABLE_, %ebx # tmp87,
  subl $16, %esp #,
  pushl 28(%esp) # y
  pushl 28(%esp) # x
  call realfoo@PLT #
  addl $24, %esp #,
  popl %ebx #
  ret
__x86.get_pc_thunk.bx:
  movl (%esp), %ebx #,
  ret
  1. , realfoo() , . , (libfoo.a). .a , .

  2. " " . -, , , , () , .

  3. . , - , () . , X11. , X 1/10 5 , musl-libc tinyX11 glibc + X11. , (, ) . , , .

  4. , . , gnu-libc (aka glibc), , , 1 , musl-libc, diet-libc uclibc - hello 10kb. glibc ( ) .

  5. , "" ; gtk , , , , , . C (, musl), , , " " "" "", C . , , GUI GTK. , perl python, , .

  6. , , . (AGPL, GPL, LGPL )... , , / .

  7. , . , libX11, , cdecl . , X11 , , ( )... cdecl, . , . . . , - .

  8. . LD_PRELOAD ( 1 - ), ( -, ) , , ( PIE ).

  9. () , , , , (, , ). , , , , - . (, libc, X11, zlib, png, glib gtk/qt/other-default-toolkit) , . Chrome . / Linux - */bin */sbin ldd objdump -x sort uniq, . , , , (, busybox, toybox, mupdf netpbm).

  10. , "DLL-" - . , , , ( , ).

.: stali ( linux)

glibc . " " , , , glibc (, uclibc ).

If you think that the static libraries are not advanced enough to provide full optimization, check out the list of Sean Barrett from individual file libraries . They can often be configured so that all functions are static, so that the binary can be built as a single compilation unit. This allows you to make several optimizations that cannot even be achieved when optimizing the connection time, but you better have an IDE that supports code folding.

0
source

All Articles