Tuesday, March 22, 2011

GCC stuff

I've often had to build some programs from source have been a little confused as to what shared libraries, static libraries, .a files, and .so files are, and how they relate to the -I -L -l options for GCC. A wonderful website that cleared all this up for me is http://www.network-theory.co.uk/docs/gccintro/index.html, here are some notes I've just scrapped down for my own reference

What is a Library
A library is
  • Useful code that has been compiled already 
  • Hence saves having to recompile that same bit of code
  • Can be used (linked to) in other programs or by other people
  • DRY (don't repeat yourself) principles - don't need to reinvent the wheel for universal functions. Eg you don't want to have to program how to draw a window with the maximize, minimize, close buttons for every program you make, so instead you can link to the windows libraries.
Static vs. Shared (Dynamic)
Static Library
  • Stored in archive .a files
  • Machine code for external functions is copied directly to your final executable
  • Makes your executable larger
  • (makes it easier to give to friends, they don't need library installed?)
Shared Library
  • Stored as shared object .so files 
  • Only a table of functions used is stored in your exe
  • when your exe starts, it loads up the linked libraries into memory (dynamic linking)
  • Final exe is smaller
  • OS will load the linked library into virtual memory, this can be used by multiple programs at once - saving memory usage
  • Can update library without recompiling code (unless the APIs change)
Linking with GCC
The long way of linking libraries is with
gcc -Wall my_prog.c /usr/lib/libm.a -o my_prog
Shorter way is with
gcc -Wall my_prog.c -lm -o my_prog
-lNAME  = /usr/lib/libNAME.a

GCC will automatically look in the directories
  1. /usr/local/lib
  2. /usr/lib
for libraries, and
  1. /usr/local/include
  2. /usr/include
for header files. 
If you library or header files aren't installed in those directories, you can include search directories with the -I  and -L options
gcc -Wall my_prog.c -L/a/new/directory/library/ -I/a/new/directory/include/ -lmy_library 
By default GCC will dynamically link to available .so over static .a files.
Environment variables
export C_INCLUDE_PATH=/a/new/directory/include
export CPLUS_INCLUDE_PATH=/a/new/directory/for/c/plus/plus/include
export LIBRARY_PATH=/a/new/directory/library
these variables allow us to now simply use
gcc -Wall my_prog -lmy_library -o my_prog
assuming my_library.a or my_library.so exists in the director /a/new/directory/library

Your program also needs to load linked libraries, if they're not located in the default directories the variable LD_LIBRARY_PATH tells your OS where to find them
export LD_LIBRARY_PATH=/my/linked/library/
Multiple Directories
If you have libraries or headers stored in multiple directories you can save the variables as
.:/dir/1:/dir/2:/dir/3
Where . = current directory

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails