2023年11月29日星期三

如何編譯Static library & 如何使用 (How to build a static library and share to others using)

這邊簡單寫一個example

====== printf.c ========

#include <stdio.h>
int print_hello()
{
    printf("This is a simple API example\n");
    return 0;

寫好這個API source code之後,先編譯出.o檔

gcc -c printf.c -o printf.o -I.


接著把.o檔,轉成.a檔

ar rcs printf.a printf.o


接著就可以寫一個主程式,去呼叫此API

如何使用此 Static Library,使用下面command編譯,可以產生test_main:

gcc main.c printf.a -o test_main -I.


也可以簡單寫一個 Makefile去做編譯(這邊解說Makefile 一小部分)
這邊是順帶的解說 Makefile語法,最主要還是要介紹,怎麼產生像是 printf.a 的 Static Library

###### Makefile 範例解說 #######

my_target: source_file.c header_file.h

gcc -o $@ $< -I.

$@: 表示目標,也就是 "my_target"

$<: 表示第一個依賴文件

沒有留言:

發佈留言

使用 lsblk 印出 emmc 每個 partition的"名字"與"size"

使用以下的command可以印出 eMMC的 partition資訊 lsblk --bytes --output name,partlabel,size   參數說明 --bytes: partition的大小,以byte的方式輸出 --output: 後面可以指定要輸出的內容...