Each Program It is essentially linked to one or more databases. For example, a program using the C function is linked to the C Runtime Library, and a GUI program is linked to the window library. In either case, you have to decide whether to link to static library (static libary) or dynamic library (Dynamic libary ).
Link to the static library will make your program bloated and difficult to upgrade, but it may be easier to deploy.
Link to the dynamic library makes your program light and easy to upgrade, but it is difficult to deploy.
Static Library
The static library puts a series of object files in the same file (similar to the. Lib file in Windows ). When you provide a static library to the linker, the connector will search for the static library, find the object files he needs, extract them, and link them to your program, just like the files you directly provide.
How to Create a static database? You can use the AR command to create an image.
The following is an example:
Test/lib/test1.c
# Include <stdio. h>
Int hello_world1 ()
{
Printf ("Hello world1/N ");
Return 1;
}
Test/lib/test2.c
# Include <stdio. h>
Void hello_world2 ()
{
Printf ("Hello World2/N ");
}
Test/APP. c
# Include <stdio. h>
Int main ()
{
Hello_world1 ();
}
Now we compile them into the directory test/lib
$ Gcc-C test1.c
$ Gcc-C test2.c
$ Ls
Test1.c test1.o test2.c test2.o
$ Ar Cr libtest. A test1.o test2.o
$ Ls
Libtest. A test1.c test1.o test2.c test2.o
The CR Mark tells ar to encapsulate the object file (archive). We can use the NM-s command to view the content of the. A file.
$ Nm-s libtest.
Archive index:
Hello_world1 in test1.o
Hello_world2 in test2.o
Test1.o:
00000000 t hello_world1
U puts
Test2.o:
00000000 t hello_world2
U puts
Now let's compile the main program
First, exit the lib directory.
$ CD ..
$ Gcc-O app. C-llib-ltest
-L specifies the Lib search path, and-L specifies the name of the Linked Library-ltest, that is, the link libtest.
$./APP
Hello world1
Hello_world1 () was found and linked from libtest. A, so there was no problem throughout the process.
dynamic library
static library) it can also be a shared library (shared Lib). The general suffix is. so. Dynamic and Static libraries are similar. They are a collection of object files, but they are organized differently. Similarly, their connection methods are different. The dynamic library is used only for links during execution and does not compile the corresponding parts into the program, A library can be used by multiple programs. Therefore, it can be called a shared library. The static library is integrated into the program, and each program uses its own library when linking.
next we will introduce how to create a dynamic library, or the previous three files, with the same layout, go to the lib directory
$ gcc-C-FPIC test1.c
$ gcc-C-FPIC test2.c
-FPIC to tell GCC to source code compile the file into a shared object, PIC (position-independent code) Non-positional dependency Code .
$ gcc-shared-FPIC-O libtest. so test1.o test2.o
combines the two files into the shared library libtest. so
exit the lib directory
$ CD ..
$ gcc-O app. c-llib-ltest
$. /APP
. /APP: Error while loading shared libraries: libtest. so: cannot open shared object file: no such file or directory
aha, we encountered an error here and did not find libtest. so, it indicates that our compilation is successful, libtest. so becomes a shared libary. The reason why the program cannot run is that libtest. So is not in the default search path.
how can this program be run?
$ LD_LIBRARY_PATH = $ PWD/lib. /APP
Hello world1
we have found our libtest. so file, and the link is successful.
references:
advenced Linux programming mark Mitchell, Jeffrey Oldham, and Alex Samuel
man nm
man ar