Use and create a static library in Linux-general Linux technology-Linux programming and kernel information. For more information, see the following. In Linux, the class libraries mainly include static libraries and dynamic libraries. Among them, the static library will automatically connect to the program when the program is connected, so once compilation is complete, the static library will not be needed. The static library usually ends with.. For example, libutil. a libuuid. a libz.. The dynamic library is not connected to the target code during program compilation, but is loaded only when the program runs. Therefore, dynamic inventory is required when the program runs. It usually ends with. so. For example, libz. so. Therefore, static databases are more efficient than shared libraries, but more space is required. It is worth noting that if there are both static and dynamic libraries, dynamic libraries are used by default during compilation. This article mainly discusses how to create and use static databases.
1. Use the dynamic library in the program
(1) if it is a standard C language library, we can not specify its library path and library name
(2) If the static library is in/lib or/usr/lib
Gcc-o test. c/usr/lib/libm. a or gcc-o test. c-lm
They have the same effect. -Lm indicates the file named libm. a or libm. so (note that the library files in Linux must start with lib ).
(3) If you want to specify the path of the static library
Gcc test. c-L/usr/lib-lm is supported.
2. Create your own static library
(1) Compile the library file testlib. c.
# Include
Void ()
{
Printf ("zyx \ n ");
}
(2) Compile a header file to declare the testlib. h function.
Void ();
(3) Compile testlib. c
Gcc-c testlib. c
(4) use ar to create an archive file
Ar crv libfirst. a testlib. o
Generate libfirst.
(5) In some systems, a content table is also generated for the static library.
Ranlib libfirst.
(6) Then you can use the static library.
For example:
[Root @ localhost Source] # vi zyx. c
[Root @ localhost Source] # gcc-o zyx. c-I./lib-L./lib-lfirst
[Root @ localhost Source] # ls
Hello HelloWorld. h static.exe zyx
HelloWorld. cpp lib StaticLibTest. c zyx. c
[Root @ localhost Source] #./zyx
Zyx
[Root @ localhost Source] #
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service