linux上編譯靜態庫與調用

           靜態庫是指在我們的應用中,有一些公共代碼是需要反覆使用,就把這些代碼編譯爲“庫”文件;在鏈接步驟中,連接器將從庫文件取得所需的代碼,複製到生成的可執行文件中的這種庫。

上面提到了靜態庫是指在我們的應用中,有一些公共代碼是需要反覆使用,那麼我們就假設一個背景,我們的開發過程中要反覆使用各種排序算法。本文將使用到封裝,繼承,多態 以及多種排序算法,初學者可以多看看。

源碼如下:

由於我們編寫的庫給別人使用肯定要給別人一個接口

SortInterface.h

#ifndef SORTINTERFACE_H
#define SORTINTERFACE_H

#define Interface  struct
//排序
Interface ISort
{ //冒泡排序
  virtual void BubbleSort(char* ptr,int len)=0;
  //快速排序
  virtual void QuickSort(char* ptr,int Low,int High)=0;
  //一個循環排序
  virtual void OneWhileSort(char* ptr,int len)=0;
};
//給他們一個獲取我們子類對象的接口(算法實現的接口)
extern "C" ISort *GetCSort(void);
#endif
上面是一個抽象類,那麼我們肯定要寫一個能夠實例化對象的類

Sort.h

#ifndef SORT_H
#define SORT_H
#include "SortInterface.h" 
class CSort:public ISort
{ 
 public:
  CSort(){}
  ~CSort(){}
  //冒泡排序
  virtual void BubbleSort(char* ptr,int len);
  //快速排序
  virtual void QuickSort(char* ptr,int Low,int High);
  //一個循環排序
  virtual void OneWhileSort(char* ptr,int len);
};
#endif
要實例化肯定要將函數實現

#include "Sort.h"
ISort* GetISort(void)
{
   return (new CSort);
}


  //冒泡排序
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 ;
}
  //快速排序
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;
}
//一個循壞排序
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;
}
好了 源碼我們已經準備好了 ,接下來我們進入編譯階段

第一步:將我們的源代碼編譯成中間文件

g++ -c Sort.cpp -o test.o//-c便是我們要將後面的源文件編譯成中間文件 -o就是給編譯的目標文件取一個名字

第二步:將我們的中間文件編譯成靜態庫

ar -rsc libtest.a test.o 

//r:在庫中插入模塊(替換)。當插入的模塊名已經在庫中存在,則替換同名的模塊。如果若干模塊中有一個模塊//在庫中不存在,ar顯示一個錯誤消息,並不替換其他同名模塊。默認的情況下,新的成員增加在庫的結尾處,可以//使用其他任選項來改變增加的位置。【1】
//c:創建一個庫。不管庫是否存在,都將創建。
//s:創建目標文件索引,這在創建較大的庫時能加快時間。(補充:如果不需要創建索引,可改成大寫S參數;如

//果。a文件缺少索引,可以使用ranlib命令添加

貌似我們的庫編譯好了?對你沒猜錯,編譯靜態庫就是這個簡單。那麼我們用一個測試代碼試一下。

main.cpp

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include"SortInterface.h"//只需要我們給的接口就可以了

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;
}
然後我們編譯可執行文件

g++  main.cpp  libtest.a  -o  a.out

//注意libtest.a的路徑

好了,大功告成,大家可以試一下了,下一篇我將講動態庫的編譯與調用




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章