# makefile應用 Copyright 2021, [月下麒麟](https://hackmd.io/@YMont/note-catalog) --- ## 概念提要 >共享函式庫(shared library)的檔案通常也是以 lib 開頭,但是其副檔名則為 **.so** >靜態函式庫(static library)的通常其檔案名稱都會以 lib 開頭,而副檔名則為 **.a** >標準函式庫的大部分函數通常是放在 libc.a裡面 (.a代表"achieve"),也被稱為靜態函式庫 。 >或者是放在共享物件動態函式庫 libc .so (.so代表"share object") 。 >這一些函式庫一般會在/lib/或是/usr/lib/裡面,或是gcc預設搜索的其他函式庫目錄。 GCC’s command line options Reference:[GCC’s command line options](https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html#Option-Index_op_letter-S) --- ![](https://i.imgur.com/mKVKlY8.png) (Reference:[GNU Compiler Collection](https://en.wikipedia.org/wiki/GNU_Compiler_Collection)) ## 實例解析 shared object ```cmake= link-gnu = $(CC) -shared $(USERFLAGS) -Wl, -h, libtest.$(SO).1 -o libtest.$(SO).1 link-aix = $(CC) -qmkshrobj -G $(USERFLAGS) -o libtest.$(SO).1 link-hpux = $(CC) -b $(USERFLAGS) -Wl,+b,$(CTIDIR)/lib -Wl,+h,libtest.$(SO).1 -o libtest.$(SO).1 ... ``` 這邊我觀察到,先從賦值後的結果來看, 發現應該是不同的電腦(HP,AIX)架構(architecture)會有不一樣的編譯指令 於此,僅針對link-gnu來探討。 Reference:[使用 gcc 自製 C/C++ 靜態、共享與動態載入函式庫教學](https://blog.gtwang.org/programming/howto-create-library-using-gcc/) 參考於**共享函式庫**裡面分享的例子, ```cmake= gcc -shared -Wl,-soname,libsum.so.1 -o libsum.so.1.0.0 sum.o ``` 再來加以反推,僅差別於link-gnu有加入變數,編譯表達的精神是一樣的 >[-shared](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-shared): Produce a shared object which can then be linked with other objects to form an executable. >[-Wl](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-Wl): Pass option as an option to the linker. libtest僅是該libary的名稱,加上副檔名.so與版本號碼.1 --- ## 實例解析 makefile rule ```cmake= libtest.a: $(OBJS) rm -f libtest.a ar rcs libtest.a $(OBJS) ``` --- 接續要再研究的是有關的Configure的應用, 像是專案的makefile裡是以**Makefile .in**; 另外,Open Source是 **Makefile .am** 且會以**autogen .sh**的方式去auto config。 Reference:[Configure Script](https://en.wikipedia.org/wiki/Configure_script) ![](https://i.imgur.com/PTzzeQ6.png) ###### tags: `Cmake`