Software development is often a very large project. It takes a lot of brain power. It is often more efficient to use libraries that others have already developed, and the following describes how to develop and use shared library files. Using libraries that others have developed, as if we want to build a car is very difficult, But if all the parts of the car already exist and are available, the work we have to do is assemble, and the assembly process must be easier than designing these parts.
The library is divided into two static function libraries and a dynamic (shared) function library. Both are collections of functions. The difference: the content of the static function library is added to the target program when compiling, and the target program has the code of the function library; While the dynamic function library is executed, the functions in the library are added to the target, and there is no code for the function library in the target program.
The library file under Linux
News: Xxxx.so
Static: Xxxx.a
Library files under Windows
News: Xxxx.dll
Static: Xxxx.lib
Common C-language libraries
Libpng
Libjpeg
Libmysql, etc.
1. Use GCC to compile a dynamic library file and use a dynamic library
gcc-shared source file-o destination file
Where-shared indicates that the result of the compilation is a dynamic library file, and the target file is often named libxxx.so.
For example, create a file for file test.c and test.h.
TEST.C:
#include <stdio.h>int print () {printf ("Hello this is lib");}
Test.h
int print ();
Use gcc-shared test.c-o libtest.so (The result of the compilation is best to use the Lib start, can be omitted when the call)
Generates a dynamic library of file libtest.so (-shared represents the generation of a dynamic function library,-o represents the generated target file).
Write a program (in CALL.C) to call the print () in this library
" libtest.h " int Main () {print (); return 0 ;}
Compile-time command
GCC call.c-ltest-l.-O C
Where-LXX represents the call to the Dynamic function library, libtest.so, if the library file is not in the format libxx.so, you can also use the-l xxx.so
-L. Indicates that the library is below the current folder
If you copy the libtest.so to/usr/lib or/usr/local/lib, you can use the command GCC call.c-ltest-o C does not need to use the-l parameter.
If you run directly./C An error occurs
#./whileobjectfilefile or directory
Reason: Although the library has been put into the System library directory or inform the function library at the current, but the system will automatically go to/etc/ld.so.cache to find the library has been recorded, not to go to the folder search. So you want to use libraries, You also need to use the command ldconfig to go to the search function library to add to the system Ld.so.cache.
If you can add the current path to/etc/ld.so.conf in the current path, and then run Ldconfig, you can add the current file to Ld.so.ccache.
You can also modify the temporary system library variable Ld_library_path. Using commands in the terminal
Export ld_library_path= $LD _library_path:.. /
(Shell command, you can use the Echo $LD _library_path to see the variables, close the terminal in the open will need to reconfigure)
After that, you can run it.
#./c
#hello this is Lib
Ldconfig is a dynamic link Library Management command. If you want to know more about it, you can check out the details of Ldconfig's commands carefully.
There is also a command ldd, which is to see what kind of dynamic library the program needs.
#ldd C
Linux-gate.so.1 = (0xb77bd000)
libtest.so = not Found
libc.so.6 =/lib/i386-linux-gnu/libc.so.6 (0xb75fc000)
/lib/ld-linux.so.2 (0xb77c0000)
2. Compile into a static library file and use
We use the above examples libtest.c and libtest.h,call.c.
First, compile the libtest.c into a static library of functions.
Gcc-c libtest.c
The difference between compiling libtest.c into LIBTEST.O, and not adding-C is that C does not link
AR crv libtest.a LIBTEST.O
AR is the creation of a static library of files. Detailed Reference man AR
Compiling the target program
GCC CALL.C Libtest.a-o C
You can then use. C + to run the.
Because the static library is added to the program when it is compiled, the xxx.a file is not required after the target is compiled.
While static libraries are relatively straightforward, the code that uses static libraries is often very large.
C language dynamic function library and static function library generation and use (under Linux environment)