The use of linux_c++ Static library and dynamic library __linux
Source: Internet
Author: User
The production and usage of static library and dynamic library (shared library) under Linux
The function library supported by Linux operating system is divided into static library and dynamic library, and dynamic library is also called Shared library. The Linux system has several important directories to store the corresponding function libraries, such as/lib/usr/lib.
static function library:
The names of such libraries are generally libxxx.a, and files compiled from static function libraries are relatively large, because all of the data in the library is integrated into the target code, his advantage is obvious, that is, the compiled execution program does not need external library support because all the functions used are already compiled into the executable file. Of course, this will also be his fault, because if the static function library changes, then your program must be recompiled, and the volume is larger.
Dynamic Function Libraries:
The names of such libraries are generally libxxx.so, dynamic library is also called Shared library; in contrast to static function library, dynamic function library is compiled without being compiled into the object code, and when your program executes to the related function, it calls the corresponding function in the library, so the executable file produced by the dynamic function library is relatively small. Because the function library is not integrated into your program, but the program runtime dynamic application and call, so the program's running environment must provide the appropriate library. The change of dynamic function library does not affect your program, so the upgrade of dynamic function library is more convenient. And if multiple applications use the same library, dynamic libraries are ideal and can reduce the size of the application.
The following is an introduction to the creation and use of Linux static function libraries:
Whether it is a static function library or a dynamic function library, it is generated by the *.O target file.
So first gcc-c ADD.C
Gcc-c SUB.C
Generate ADD.O SUB.O
The static function library is created by the AR command
This example: AR-CR libaddsub.a add.o SUB.O
C-Create meaning
-R replace means that a module with the same name is replaced if the inserted module name already exists in the library. If a module in several modules does not exist in the library, AR displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the library, and other options can be used to change the added position.
To this static function library has been created.
How to: Compile Main.c by Gcc-o main main.c-l.-laddsub to integrate the static function library into main.
which
-l Specifies the location of the static function library for lookup, note that there is a '. ' behind L, which means that the static function library is found in this directory.
-l Specifies the name of the static function library, because the static function library is named Lib***.a, where Lib and. A are ignored.
According to the features of the static function library, main can still run after the deletion of the LIBADDSUB.A, because the contents of the static library are already integrated in.
Creation and use of dynamic function libraries
Gcc-shared-fpic-o libaddsub.so ADD.C SUB.C
-fpic: Generate location-independent code
-shared: Building shared libraries
Generate the libaddsub.so dynamic function library with the above command.
Gcc-o out main.c-l.-laddsub
It is not immediately./out, because when the dynamic function library is used, it looks for the dynamic function library under the/usr/lib/lib directory, and the library we are building is not in it at this time.
There are several ways to make a successful run of this time:
The most straightforward and easiest way is to pull libaddsub.so into/usr/lib or/lib.
There is also a way to assume that libaddsub.so in/home/linux/addsub
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.