[Book Recommendation] "Linux C and C + + first-line development practice (2nd Edition)" _linux c and c + + first-line development practice pdf-CSDN blog
Linux C and C + + first-line development practice (2nd Edition) (Linux Technology Series) (Zhu Wenwei, Li Jianying) [Abstract Book Review Trial reading] - Jingdong Books ()
10.3.1 Basic concepts of static libraries
Static library files have a .a suffix and are generally named under Linux. When there is a program that uses a particular static library, during the linking step, the linker copies the code obtained from the static library file into the generated executable, i.e., all the functions in the entire library are linked into the executable. Therefore executables using static libraries are usually larger. However, the advantage of using static libraries is also very obvious, i.e., the executable program does not need the support of files related to the library when it is finally run, because all the functions used have already been compiled in, and the executable file can be run directly. Of course, sometimes this is a disadvantage, for example, if the contents of the static library change, then the program (the caller) must be recompiled.
10.3.2 Creating and using static libraries
Static libraries are usually created using the ar command. With the ar command you are actually combining a number of target files (.o) into a single static library.The steps to create a static library on Linux are as follows:
(1) Edit the source file (e.g. .c or .cpp file).
(2) Generate the target file (i.e., .o file) via gcc -c or g++ -c.
(3) Archiving target files with ar to generate static libraries.
(4) Write a header file in conjunction with the static library. The contents of the file are the declarations of functions, variables, or classes that are provided for outside use.
To learn to create static libraries, mainly to learn the use of the ar command. ar command can not only create static libraries, you can also modify or extract the existing static library information. Its common usage is as follows:
ar [option] ...
Among them, option is the option of the ar command; is the name of the generated static library file, xxx is usually the name we set up, lib is a habit, static libraries usually start with lib; after the,, is to be archived into the static library in the target code file, there can be more than one, so the back of the ellipsis.
Commonly used options are listed below:
(1) Option c: Used to create a library. The library will be created whether it exists or not.
(2) Option s: create an index of the target file, this can speed up the time when creating larger libraries. If you don't need to create an index, you can change the parameter to uppercase S. If the .a file lacks an index, you can also add it with the ranlib command.
(3) Option r: Inserts a module into the library; if the name of the inserted module already exists in the library, the module with the same name will be replaced. If one of several modules does not exist in the library, ar displays an error message and does not replace the other modules with the same name. By default, new members are added at the end of the library; any other option can be used to change the location of the addition.
(4) Option t: shows which target files are in the library file. Note that only the names are shown.
(5) Option tv: Displays which target files are in the library file. Information displayed includes file name, time, size, etc.
(6) Option s: Display the index table in the static library file.
To use static libraries is easy, so let's look at an example of generating a static library and using it.
[Example 10.1] Creating and using static libraries (g++ version)
(1) Open Visual Studio Code and create a new source file with the following contents:
#include <>
#include <iostream>
using namespace std;
void f(int age)
{
cout << "your age is " << age << endl;
printf("age:%d\n",age);
}
The code is very simple. This source file is mainly used as a static library. We first upload the file to Linux by typing the following command at the command line:
# g++ -c
At this point the target file will be generated in the same directory. Enter the following command to generate the static library:
# ar rcs
Where ar is the command for static library creation and c means create, both explained earlier in rs.
At this point a static library file will be generated in the same directory. Note that the first 3 digits of the name of the .a file to be generated should be lib, otherwise the library may not be found when linking.
(2) Now that the static library has been generated, let's write a separate source file to use the library function f. Open Visual Studio Code, create a new file, and enter the following code:
extern void f(int age); // declare the function to be used
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
f(66);
cout << "HI" << endl;
return 0;
}
The code is very simple. First declare the f, then you can use it in the main function. Save the file and upload it to Linux, taking care to put it in the same directory as, then compile and run it from the command line:
# g++ -o main -L. -ltest
# ./main
your age is 66
age:66
HI
The compilation ran successfully. Where -L is used to tell g++ where to look for library files, it is followed by a dot (.) , which means to look for library files under the current directory; -l is used to specify a specific library, where lib and .a don't need to be explicitly written out, g++ or gcc will automatically go looking for them, which is also the reason that when we generated static libraries earlier, the filenames of static libraries should be prefixed with lib. By default, g++ or gcc searches for dynamic library (.so) files first, and then looks for static library (.a) files after failing to find them. There are no dynamic library files in the current directory, so static library files can be found.
The process of using static libraries is similar for gcc and g++, and an example for the gcc version is given below.
[Example 10.2] Creating and using static libraries (gcc version)
(1) Open Visual Studio Code and create a new source file with the following contents:
#include <>
void f(int age)
{
printf("age:%d\n",age);
}
This source file is mainly used as a static library. We first upload the file to Linux by typing the following command at the command line:
# gcc -c
At this point the target file will be generated in the same directory. Enter the following command to generate the static library:
# ar rcs
At this point static library files are generated in the same directory.
(2) Now that the static library has been generated, let's write a separate source file to use the library function f. Open Visual Studio Code, create a new file, and enter the following code:
extern void f(int age); // declare the function to be used
int main(int argc, char *argv[])
{
f(66);
return 0;
}
The code is very simple, first declare the f and then you can use it in the main function. Save the code and upload it to Linux, taking care to put it in the same directory as, then compile and run it from the command line:
# gcc -o main -L. -ltest
# ./main
age:66
The compilation ran successfully. Where -L is used to tell gcc where to look for library files, it is followed by a dot (.) , which means to look for library files under the current directory; -L is used to specify specific libraries, where lib and .a don't need to be explicitly written out, and g++ or gcc will automatically go looking for them. By default, g++ or gcc searches for dynamic library (.so) files first, and then looks for static library (.a) files after failing to find them. There are no dynamic library files in the current directory, so the static library files can be found.