Skip this Video
Download Presentation
Static Libraries

Loading in 2 Seconds...

play fullscreen
1 / 11

Static Libraries - PowerPoint PPT Presentation

  • Uploaded on

Static Libraries. inside a static library, you are likely to find that it contains many modules with only one function per module. This gives the linker maximum opportunities to remove unnecessary code.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Static Libraries' - vance-wynn

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
static libraries
Static Libraries
  • inside a static library, you are likely to find that it contains many modules with only one function per module.
    • This gives the linker maximum opportunities to remove unnecessary code.
    • The linker reads the object files contained in the archive one by one when it tries to resolve external symbols. The linker is free to discard any object files that are not required.
dynamic libraries
Dynamic Libraries
  • Unlike a static library, which is just an archive of object files, a dynamic library is itself an object file.
  • Dynamic libraries are created by the linker, unlike static libraries, which are created by the compiler and the ar command.
  • When you link with a dynamic library, the compiler does not copy any object code into your executable file.
    • Instead, it records information about the dynamic library in your executable (stub) so that it can be dynamically linked at runtime.
    • This means that part of the link process is deferred until runtime.
    • When a dynamically linked program executes, the program performs the final linking stages before it enters main.
  • When the linker sees a static library, it is free to pull in only the object files that your program actually needs, which keeps the executable as small as possible.
    • When your code links with a shared object, however, it’s all or nothing. linker cannot discard pieces of the shared object, so yourprocess’s memory will map all the code contained in the shared object, whether itcalls it or not.
    • 這是為了在有VM時,OS方便管理記憶體映射。若在沒有VM下,使用動態函式庫就可能過於昂貴了!!
creating a dynamic library
Creating a Dynamic Library
  • hello.c for the main program and empty.c, be an empty shared object.

$ cat <<EOF > hello.c

#include <stdio.h>

int main() { printf("Hello World\n"); }


$ cat /dev/null > empty.c

Creating shared object

$ gcc -shared -fpic -o empty.soempty.c

To link hello with this shared object,

$ gcc -o hello

To execute, you need to set LD_LIBRARY_PATH環境變數

$ LD_LIBRARY_PATH=. ./hello

Hello World

  • we will create a new shared object that does nothing but consume memory. Let’s call it piggy.c:

$ cat <<EOF > piggy.c

>char bank[0x100000];



  • hello.c program that just waits for us to press Ctrl+C so we can examine it while it runs:

$ cat <<EOF > hello.c

#include <unistd.h>

int main(){ pause(); }



$ gcc -o hello hello.cpiggy.c

$ size hello

text data bssdec hex filename

1117 264 1048608 1049989 100585 hello

$ nm -S hello | grep bank

080496c0 00100000 B bank

  • When the modules are linked like this, the memory consumption is visible withthe size command. The nm command shows that the array bank is present and consuming1MB of space.

$ gcc -c piggy.c

$ arclqlibpig.apiggy.o

$ gcc -o hello hello.c -L ./ -lpig

$ size hello

text data bssdec hex filename

1117 264 4 1385 569 hello

$ nm -S hello | grep bank

  • This time, the size of the executable is noticeably smaller because the bank arrayis not included, which tells us that the piggy.o module was excluded.
    • The linker isable to determine that there are no references to piggy.o, so it does not link it withthe executable.
process memory map
  • We’ll run it using the pmap commandto get a look at the process’s memory map:

$ ./hello &

$ jobs -x pmap -q %1

7382: ./hello

08048000 4K r-x-- /hello

08049000 4K rw--- /hello

b7dab000 4K rw--- [ anon ]

b7dac000 1160K r-x-- /

b7ece000 36K rw--- /

b7ed7000 8K rw--- [ anon ]

b7ee7000 4K r-x-- /

b7ee8000 4K rw--- /

b7ee9000 1032K rw--- [ anon ] //consumed by bank array in virtual address space

b7feb000 84K r-x-- /

b8000000 4K rw--- /

bffeb000 84K rw--- [ stack ]

ffffe000 4K ----- [ anon ]

  • So linking with a shared object can force you to consume virtual memory that you otherwise would not use

By default, the compiler uses dynamic linking, which means that if it can choose between a static library and a dynamic library, it will choose the dynamic library. If you want to link exclusively with static objects, you must specify the –static option to gcc/g++.

build process
Build Process
  • A typical source distribution created with the GNU build tools includes a configure script, which is used by the end user to create a Makefile.

$ ./configure

$ make

$ make install