- linker
- combine .o files
- resolve undefined symbols
- non-local function
- global variable
- from static library
- shared (dynamic)library modules linked when inside cpu
- use library
- #include
- <>
- specify with -I <dir of lib> / -L<dir> / -l(lib name)when compile
- “” user specific includes(.h write by user)
- static
- library modules stored inside executable file
- dynamic
- library modules remain in a shared library
- pro: smaller executable file and memory image
- step to create a library
- compile library file(.c/.cc….) to .o file (gcc/g++ -c )
- ar rcs (libName.a) *.o
- r(insert into library with replacement)
- c(create new archive)
- s(write object in to archive)
- t(list object used to create archive)
- use nm to list symbol inside archive(library supported datetype/functions)(optional)
- install –group=* –mode=664(access premission(exp. 777)) *.a <dir(/bin..)>
- cp is also fine
- when compile program need lib add as before, add -static -L<dir> l<libName>
- if it add dynamicly, the executable file created is smaller than the one use static
- basic idea: mulit-file use same code
- al link time, symbol resolved
- lib modules remain in archive
- when run, dynamic loader “attach” library modules with program
- if second pogram also need, just share copy in memory
- why
- reduce program size
- save RAM
- con.
- slower first time start up
- can not compile in another pc(if lib not installed)
- shared library
- DLL files on Windows
- .gylib on mac
- .so on linux
- library must be Re-entrant and PIC
- In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again (“reentered”) before its previous invocations complete execution.(若一个程序或子程序可以“安全的被并行执行(Parallel computing)”,则称其为可重入(reentrant或re-entrant)的。即当该子程序正在运行时,可以再次进入并执行它(并行执行时,个别的执行结果,都符合设计时的预期)。可重入概念是在单线程操作系统的时代提出的。一个子程序的重入,可能由于自身原因,如执行了jmp或者call,类似于子程序的递归调用;或者由于硬件中断,UNIX系统的signal的处理,即子程序被中断处理程序或者signal处理程序调用。重入的子程序,按照后进先出线性序依次执行。)
- 若一个函数是可重入的,则该函数:
- 不能含有静态(全局)非常量数据。
- 不能返回静态(全局)非常量数据的地址。
- 只能处理由调用者提供的数据。
- 不能依赖于单实例模式资源的锁。
- 不能调用(call)不可重入的函数(有呼叫(call)到的函数需满足前述条件)。
- depend on program logic
- keep non-constant variables in dynamic-allocated or automatic-allocated memory
- PIC(position-independent code)
- a body of machine code that, being placed somewhere in the primary memory, executes properly regardless of its absolute address.
- commonly used in shared libraries
- shared Library
- code must be