Use ar to make static libraries

One, know the static library

  1. Use .a as the suffix, generally named libxxx.a under Linux;
  2. When the program uses a static library, the linker (ld) copies all functions of the entire libxxx.a to the executable file;

2. Advantages and disadvantages

Advantages: When using a static library, after compiling into an executable file, it does not depend on the library at runtime.

Disadvantages: The executable file is large, and the static library has changed, the executable file has to be regenerated.

Three, how to create and use static libraries

Tool: ar, package the object file .o into a separate static library.

Steps to make a static library under Linux:

  1. Edit source code files (.c, .cpp)
  2. Generate object file *.o through gcc -c *.c or fold g++ -c *.cpp
  3. Use ar to package object files and generate static libraries
  4. Provide header files for static libraries (that is, *.h files such as service interfaces provided by static libraries)

The use of the ar command:

语法:ar [-]{dmpqrtx}[abcfilNoPsSuvV] [membername] [count] archive files...

parameter:

 Instruction parameter 

  -d delete member files in the library file

 -m change the order of member files in the library file 

-p displays the contents of the member files in the library file 

-q append the file to the end of the library file 

-r insert the file into the library file

-t displays the files contained in the library file 

-x Remove member files from library files

Option parameter

a<Member file> Insert the file after the member file specified in the library file.

b<Member file> Insert the file before the member file specified in the library file.

c Create library files.

f In order to avoid the long file name is not compatible with the ar command of other systems, this parameter can be used to cut off the long member file name to be put in the library file.

i<member file> Insert the member before the member file specified in the library file.

o Keep the date of the file in the library file.

s If the library file contains object mode, this parameter can be used to create a symbol table for the file to be saved.

S does not generate a symbol table.

u Only insert files with a newer date into the library file.

v Display detailed information when the program is executed.

V Display version information.

Four, chestnuts

test.h

int add(int a, int b);

test.cpp

#include "test.h"

int add(int a, int b)
{
    return (a + b);
}

Compile:

[xy@xunye ar]$ g++ -c test.cpp 
[xy@xunye ar]$ ls
test.cpp  test.h  test.o

Create a static library:

[xy@xunye ar]$ ar rcs libtest.a *.o
[xy@xunye ar]$ ls
libtest.a  test.cpp  test.h  test.o

Use static libraries:

main.cpp

#include "test.h"
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    cout << add(3, 2) << endl;

    return 0;
}

Create an executable file:

[xy@xunye use_lib]$ ls
libtest.a  main.cpp  test.h
[xy@xunye use_lib]$ g++ -o main main.cpp -L. -ltest
[xy@xunye use_lib]$ ls
libtest.a  main  main.cpp  test.h
[xy@xunye use_lib]$ ./main 
5

Description: -L specifies the path for g++ to search for static libraries; "." means to search under the current path

          -l specifies a specific library, among which lib and .a can be written without displaying

[Note] The order of searching libraries for gcc or g++: first find the dynamic library .so, and then find the static library .a if you cannot find it.

Guess you like

Origin blog.csdn.net/xunye_dream/article/details/110727531