Creating library with GCC
七月 24, 2007 at 11:22 上午 | In Default, Technology, compiler |A Short Definition
A static library is basically a collection of object files.During the linking phase of compilation,the static library,or the object files in it, will be simply copied into the target executable file.Static libraries usually ends with a “.a” suffix in UNIX variants(including MinGW) and “.lib” in Windows.
A shared library,also called dynamic library(on Windows,they are the familiar dlls,the origin of “DLL Hell”) is “dynamically” linked into program in runtime.Why I use “dynamically” here is that during compilation the object files contained in shared library are not inserted into the final executable file;instead, when the program is started, a program in the system (called a dynamic loader) checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory.
Creating Static Library
Suppose you have some object files with the name util1.o,util2.o,….Then then following command will create a static library “libutil.a” and puts all the objs into it:
ar rcs libutil.a util1.o util2.o ...
Note that the static library must start with “lib” and have the suffix “.a”
ar can be used to creating,modifying and checking static libraries.
Creating Shared Library
The file format of shared library depends on specific architecture.On *nix system,shared library always have suffix “.so“(sometimes with version number) while on Windows “.dll“.
The following command creates a shared library libutil.dll
gcc -shared libutil.dll util1.o util2.o ...
Note again: the library must start with the three letter lib
Linking against Shared Library
Suppose we write a program main.c which calls a function *** in libutil.dll
gcc -o main main.c -L. -lutil
Note: the first three letters (the lib) must not be specified, as well as the suffix (.dll)
Option “-L.’ specifies the path where linker search for libraries.Therefore,to ensure the above command work,libutil.dll must be put in the same path as the source code.
More information can be found here.
阅读(862 次)
1 条评论 »
这篇文章上的评论 RSS feed TrackBack URI
留下评论
Zero Sound
, powered by 赛族 & WordPress MU | Theme: Pool by Borja Fernandez.
Entries and comments feeds.




MSN:

still alive?
thought u r in Italy now
评论 由 VenomCK — 七月 24, 2007 #