如何建立和使用Linux靜態/動態連結程式庫

來源:互聯網
上載者:User

和Windows系統一樣Linux也有靜態/動態連結程式庫,下面介紹建立和使用方法:

假設有下面幾個檔案:

標頭檔String.h,聲明相關函數原形,內容如下:

Strlen.c:函數Strlen的實現,擷取給定字串的長度,內容如下:

Strlnen.c:函數StrNlen的實現,擷取給定字串的長度,如果輸入字串的長度大於指定的最大長度,則返回最大長度,否者返回字串的實際長度,內容如下:

產生靜態庫:

利用GCC產生對應目標檔案:

gcc –c Strlen.c Strnlen.c

如果對應的檔案沒有錯誤,gcc會對檔案進行編譯產生Strlen.o和Strnlen.o兩個目標檔案(相當於windows下的obj檔案)。然後用ar建立一個名字為libstr.a的庫檔案,並把Strlen.o 和Strnlen.o的內容插入到對應的庫檔案中。,相關命令如下:

ar –rc libstr.a Strlen.o Strnlen.o

命令執行成功以後,對應的靜態庫libstr.a已經成功產生。

<span style="font-size:18px;"><strong>/*********************************** Filename : String.h Description : Author   : HCJ Date     : 2006-5-7 ************************************/            int Strlen(char *pStr);  int StrNlen(char *pStr, unsigned long ulMaxLen);                     /************************************** Filename    : get string length Description  :  Author      : HCJ Date        : 2006/5/7 **************************************/#include<stdio.h>  #include<assert.h>        int Strlen(char *pStr)  {      unsigned long ulLength;      assert(NULL != pStr);            ulLength = 0;      while(*pStr++)      {          ulLength++;      }            return ulLength;  }                     **********************************************  Fileneme: mystrnlen.c  Description: get input string length,if string large               max length input return max length,               else real length  Author: HCJ  Date  : 2006-5-7  **********************************************/        #include<stdio.h>  #include<assert.h>        int StrNlen(char *pStr, unsigned long ulMaxLen)  {      unsigned long ulLength;            assert(NULL != pStr);            if(ulMaxLen <= 0)      {          printf("Wrong Max Length!\n");          return -1;      }            ulLength = 0;      while(*pStr++ &&  ulLength < ulMaxLen)      {          ulLength++;      }            return ulLength;  }  </strong></span>

本文URL地址:http://www.bianceng.cn/OS/Linux/201410/45420.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.