Static libraries are common codes that need to be reused in our applications and compiled into libraries; in the linking step, the connector takes the required code from the library file and copies it to the libraries in the generated executable files.
As mentioned above, static libraries are common codes that need to be used repeatedly in our applications. So let's assume a background and use various sorting algorithms repeatedly in our development process. This article will use encapsulation, inheritance, polymorphism and a variety of sorting algorithms, beginners can see more.
The source code is as follows:
Because we write libraries to be used by others, we must give others an interface.
#ifndef SORTINTERFACE_H #define SORTINTERFACE_H #define Interface struct //sort Interface ISort { //Bubble sort virtual void BubbleSort(char* ptr,int len)=0; //Quick sort virtual void QuickSort(char* ptr,int Low,int High)=0; //A cyclic sort virtual void OneWhileSort(char* ptr,int len)=0; }; //Give them an interface for getting our subclass objects (the interface for algorithm implementation) extern "C" ISort *GetCSort(void); #endif
The above is an abstract class, so we must write a class that can instantiate objects.
Sort.h
#ifndef SORT_H #define SORT_H #include "SortInterface.h" class CSort:public ISort { public: CSort(){} ~CSort(){} //Bubble sort virtual void BubbleSort(char* ptr,int len); //Quick sort virtual void QuickSort(char* ptr,int Low,int High); //A cyclic sort virtual void OneWhileSort(char* ptr,int len); }; #endif
To instantiate, functions must be implemented
#include "Sort.h" ISort* GetISort(void) { return (new CSort); } //Bubble sort void CSort::BubbleSort(char* ptr,int len) { for(int i=0;i<len-1;i++) { for(int j=i+1;j<len;j++) { if(ptr[i]>ptr[j]) { ptr[i]+=ptr[j]; ptr[j]=ptr[i]-ptr[j]; ptr[i]=ptr[i]-ptr[j]; } } } return ; } //Quick sort void CSort::QuickSort(char* ptr,int Low,int High) { if(Low>High) return; char temp=ptr[Low]; int tempLow=Low; int tempHigh=High; while(tempLow<tempHigh) { while(tempLow<tempHigh&&temp<=ptr[tempHigh]) { tempHigh--; } char tempData=ptr[tempHigh]; ptr[tempHigh]=ptr[tempLow]; ptr[tempLow]=tempData; while(tempLow<tempHigh&&temp>ptr[tempLow]) { tempLow++; } tempData=ptr[tempHigh]; ptr[tempHigh]=ptr[tempLow]; ptr[tempLow]=tempData; } QuickSort(ptr,Low,tempLow-1); QuickSort(ptr,tempLow+1,High); return; } //A Bad Order void CSort::OneWhileSort(char* Array,int len) { int temp=0; int i=0; while (i < len) { temp++; if (i == 0 || Array[i - 1] <= Array[i]) { i+=temp; temp=0; } else { int t = Array[i]; Array[i] = Array[i - 1]; Array[i - 1] = t; i--; } } return; }
Well, we're ready for the source code, and then we're going to go into the compilation phase.
Step 1: Compile our source code into intermediate files
G++-c Sort.cpp-o test.o//-c is that we need to compile the following source files into intermediate files-o is to name the compiled target files.
Step 2: Compile our intermediate files into static libraries
ar -rsc libtest.a test.o
// r: Insert modules (replacements) into libraries. When the inserted module name already exists in the library, the module with the same name is replaced. If one of the 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 optional options can be used to change the location of the addition. [1]
// c: Create a library. Whether the library exists or not, it will be created.
// s: Creating target file indexes speeds up the creation of larger libraries. (Supplement: If you do not need to create an index, you can change it to uppercase S parameter; for example
/ / fruit. a file lacks an index and can be added using the ranlib command
Looks like our library is compiled? It's easy to compile static libraries for you to guess right. So let's try it out with a test code.
#include <iostream> #include <stdio.h> #include <unistd.h> #include"SortInterface.h"//Just need the interface we give you. using namespace std; int main() { ISort*TempRule =GetISort(); char a[20]{12,1,9,2,0,11,7,19,4,15,18,5,14,13,10,16,6,3,8,17}; TempRule->QuickSort(a,0,19); for(int i=0;i<20;i++) { printf("%d\n",a[i]); } return 0; }
Then we compile the executable file
g++ main.cpp libtest.a -o a.out
Note the path of libtest.a