Most of the time in our daily development, we actually work with third-party libraries or system libraries. In the field of Android audio and video development, FFmepg, OpenCV, OpenGL and other open source libraries are generally used, and we generally compile into dynamic libraries for our programs to use. For Unix-like systems, the static library is.a and the dynamic library is.so. In Windows, the static library is.lib and the dynamic library is.dll.

Statically linked library

Review the four steps of compiling a program:

Precompile -> compile -> Assemble -> Link

Static libraries and dynamic libraries behave differently during the linking phase. Static libraries will link and package the object file generated by assembly together with the referenced library into the executable file during the linking phase. A static library is simply a collection of object files that can be packaged and generated using ar tools. Static Kurt points are:

  • Static library links to functions are completed at compile time
  • The program is no longer associated with the library at runtime
  • Waste of resource space because all related object files are linked into an executable

Let’s look at an example:

// Header file math.hpp#ifndef Math_hpp
#define Math_hpp

#include <stdio.h>

extern int add(int a, int b);


#endif /* Math_hpp */

Copy the code
Math.hpp implementation file#include "Math.hpp"

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


Copy the code
// main. CPP Main entry#include <iostream>

extern int add(int a, int b);

int main(int argc, const char * argv[]) {
    // insert code here...
    
    using std::cout;
    using std::endl;

    cout << "add 2 3: " << add(2, 3) << endl;

    
    return 0;
}

Copy the code

Use clang generation to compile into object files

clang++ -c Math.cpp

Then use the ar command to package the object file and generate the static library file libmath.a

ar -r libmath.a Math.o

The ar command has some common parameters:

  • -t: Displays file names in the archive in sequence
  • -d: deletes a specified file from the archive

Linked static library

clang++ main.cpp -L. -lmath -o main

After the system is successfully generated, run the./main command

Explain the parameters:

  • -l: indicates the directory where the library to be linked resides. If this parameter is not specified, the system searches for the directory in usr/lib or usr/local/lib
  • -l: specifies the dynamic or static library for linking. If there is a dynamic library, it will be linked first

Dynamically linked library

The reason for using a dynamic library is because static libraries consume memory, and updating static libraries is a disaster. If the library source code changes, static libraries will have to be rebuilt.

Dynamic Kurt points are as follows:

  • Some library functions are lazily loaded until they are needed
  • Dynamic libraries can be shared by multiple programs simultaneously, saving memory

Using the above example to show the use of dynamic library, first, generate dynamic link library file

clang++ Math.o -shared -fPIC -Wall -o libmath.so

After the dynamic link library file is generated, run the following command to generate the executable file

clang++ main.cpp -L. -lmath -o main

Perform. / main

Parameter Description:

  • -shared: indicates that a dynamically linked library is generated
  • -fPIC: generates position-independent code for compiling shared libraries. Can be loaded in any memory space
  • -Wall: generates all warning information