Sunday, December 04, 2011

How to compile library and link?


1) gcc -c hello.c
Compile hello.c to object file.
2) ar rcs libhello.a hello.o
(or ar ruv hello.a hello.o)
Link object to static library.
3) gcc test.c -o test libhello.a
Compile executable file and link libhello.a
To create the shared (*.so) library, use these steps
1) gcc -fpic -c hello.c
2) gcc -shared -o libhello.so hello.o
3) gcc test.c -o test -L. -lhello
ar x /usr/lib/libx86.a
extract object file from libx86.a
ar t /usr/lib/libx86.a
Display a table listing the contents of archive…
ar – create, modify, and extract from archives
t Display a table listing the contents of archive, or those of the files listed in member… that are present in the archive. Normally only the member name is shown; if you also want to see the modes (permissions), timestamp, owner, group, and size, you can request that by also specifying the v modifier.
If you do not specify a member, all files in the archive are listed. If there is more than one file with the same name (say, fie) in an archive (say b.a), ar t b.a fie lists only the first instance; to see them all, you must ask for a complete listing—in our example, ar t b.a.
x Extract members (named member) from the archive. You can use the v modifier with this operation, to request that ar list each name as it extracts it.
If you do not specify a member, all files in the archive are extracted. Files cannot be extracted from a thin archive.
nm – list symbols from object files

No comments: