編譯Linux使用的.a庫檔案

來源:互聯網
上載者:User

標籤:庫   linux   c   

編譯Linux使用的.a庫檔案

 

首先是需要編譯成.a的源檔案

 

hello.h:

#ifndef __INCLUDE_HELLO_H__#define __INCLUDE_HELLO_H__void hello(const char *name);#endif // end __INCLUDE_HELLO_H__

hello.c

#include "stdio.h"void hello(const char *name){    printf("Hello world %s\n", name);}


和一個在linux平台上做測試的main.c

#include "hello.h"int main(){    hello("everyone");        return 0;}


在Linux上面做測試,編譯成.a檔案,主要利用.o檔案和ar命令


1、首先產生.o檔案:

gcc -c hello.c

這樣原始碼的目錄下就會產生一個hello.o



2、利用ar命令,從.o檔案中建立.a檔案

ar cr libhello.a hello.o

這樣就可以產生.a檔案了,注意,所要產生的.a檔案的名字前三位最好是lib,否則在連結的時候,就可能導致找不到這個庫



3、在linux下測試使用

編譯main.c,並讓hello.a連結到main中

gcc main.c -L. -lhello -o main(注意這裡-L後面有個.)

這樣在當面目錄下面就出現了可執行程式main,直接運行就是我們索要的結果了



編譯Linux使用的.a庫檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.