A library is a package of code which can be used by other program. First, libraries are compiled. You don't have to compile them when you are using them. Second, because the complied libraries are in machine language, it can prevent other people to know the source code. This is important for business use. But it's not good for open source.
-----------------------------------------------------------------------------------------------------
What is libraryActually, library is just a collection of header(C++) files or sub functions(maybe Fortran).
There are two types library.
- Static library(archive library). In linux, statics lib has a .a extension. When you compile a program using static library, the program will copy all the funcitonally to the program. So your executable file is very big. One advantage of static library is the program does not have to find the lib when it's running. This is very convenient when you compile the code in one machine and run the program in another machine and you don't want to install the lib in that machine.
- Dynamic library(shared library). Dynamic library has a .so extension in Linux. Dynamic lib will be linked when the program is running. So you don't have to copy all the libs to your executable file. That is a advantage. Because the library you are using may be very big(GB).
------------------------------------------------------------------------------------------------------
Generate library
- To create a static library,
ar rcs my_library.a file1.o file2.o
This command adds the objects files file1.o and file2.o to the static library. - Create a Shared library. First , create the object files using the gcc -fPIC of -fpic flag. The -fPIC and -fpic option enable 'position independent code' generation. Here is an example.
gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc
-g means debugging information. -Wall will generate warnings. -c is compilation.
--------------------------------------------------------------------------------------------------------
Use Library
If you want to use the shared library, you have to tell the computer where the library is. Actually you have to how the Linux system to find lib.
- Default shared lib directory. The default dir is /usr/lib, which means the system will first search the libs in /usr/lib. Then you have to run ldconfig.
ldconfig -n directory_with_shared_libraries
- You can use LD_LIBRARY_PATH to tell the system to find the shared libraries before the usual places.
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH my_program
- ldd can display the dependence of a shared library.
ldd /usr/bin/mesg
It will display
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7eaf000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7feb000)
---------------------------------------------------------------------------------------------------------
My MakefileThis is a Makefile I am using.
##################################
# root variables
ROOTCFLAGS := $(shell root-config --cflags)
ROOTLIBS := $(shell root-config --libs) -lRooFit -lHtml -lCore -lCint -ldl -lTree \
-lGpad -lGraf -lHist -lMatrix -lPostscript -lMinuit -lMinuit2 -lPhysics
# Programs
CXX = g++
F77 = f77
CXXFLAGS = -g -ggdb -Wall -fPIC -Wno-deprecated
MY_CXXFLAGS = -static
LDFLAGS = -g
SOFLAGS = -shared
# do yourself a favor, use gcc for linking, and not ld
LD = CXX
# local Includes
INCDIR = -Iinclude
# Assign or Add variables
CXXFLAGS += $(ROOTCFLAGS)
CXXFLAGS += $(INCDIR)
# Assigan or add variables
LIBS += $(ROOTLIBS)
validation: src/validation.C clean
$(CXX) $(CXXFLAGS) $(MY_CXXFLAGS) -c src/validation.C -o lib/validation.o
$(CXX) lib/validation.o -o bin/validation $(LDFLAGS) $(LIBS)
clean:
rm -rf bin/* lib/*
$(shell root-config --libs) is a fantastic bash command. When you run the executable file, this command will be evaluated and will give the all the libs related to
root(which is a analysis framework of high energy physic). In my case, the result of this command is
-L/afs/cern.ch/sw/lcg/external/root/5.18.00/slc4_ia32_gcc34/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -pthread -lm -ldl -rdynamic
--------------------------------------------------------------------------------------------
Program Library HOW TOStatic and Dynamic Libraries Analysis of static and shared libraryProgramming of static and shared library