在鏈接階段中,所有對應(yīng)于源文件的 .o 文件、"-l" 選項指定的庫文件、無法識別的文件名(包括指定的.o目標(biāo)文件和.a庫文件)按命令行中的順序傳遞給鏈接器。
下面看一下,鏈接的過程是怎樣的:
gcc -v -o helloworld helloworld.o Usingbuilt-inspecs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exeTarget: mingw32Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nlsThread model: win32gcc version 6.3.0 (MinGW.org GCC-6.3.0-1) COMPILER_PATH=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../libexec/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/LIBRARY_PATH=d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../lib/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../COLLECT_GCC_OPTIONS='-v' '-o' 'helloworld.exe' '-mtune=generic' '-march=i586' d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:UsersMrBluAppDataLocalTempcceFgn0y.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -o helloworld.exe d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0 -Ld:/software/mingw/bin/../lib/gcc -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. helloworld.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.oCOLLECT_GCC_OPTIONS='-v' '-o' 'helloworld.exe' '-mtune=generic' '-march=i586'
crt2.o 、crtbegin.o、crtend.o是gcc加入的系統(tǒng)標(biāo)準(zhǔn)啟動文件,對于一般應(yīng)用程序,這些啟動是必須的。
-Ldir:在庫文件的搜索路徑列表中添加dir目錄。
-lname:添加鏈接庫文件。
靜態(tài)鏈接與動態(tài)鏈接
庫有兩種:靜態(tài)庫(.a、.lib)和動態(tài)庫(.so、.dll)。
window上對應(yīng)的是 .lib、.dll。
linux上對應(yīng)的是 .a、.so
如果函數(shù)庫的一份拷貝是可執(zhí)行文件的物理組成部分,稱之為靜態(tài)鏈接。
靜態(tài)鏈接當(dāng)鏈接程序時,需要使用的每個庫函數(shù)的一份拷貝被加入到可執(zhí)行文件中。
靜態(tài)鏈接使用靜態(tài)庫進(jìn)行鏈接,生成的程序包含程序運(yùn)行所需要的全部庫,可以直接運(yùn)行,不過靜態(tài)鏈接生成的程序體積較大(即使是在靜態(tài)鏈接中,整個庫文件也并沒有全部裝入到可執(zhí)行文件中,所裝入的只是需要的函數(shù))。
如果可執(zhí)行文件只是包含了文件名,讓載入器在運(yùn)行時能夠?qū)ふ页绦蛩枰暮瘮?shù)庫,稱之為動態(tài)鏈接。
動態(tài)鏈接允許系統(tǒng)提供一個龐大的函數(shù)庫集合,可以提供許多有用的服務(wù),程序在運(yùn)行時尋找它們。
動態(tài)鏈接使用動態(tài)鏈接庫進(jìn)行鏈接,生成的程序在執(zhí)行的時候需要加載所需的動態(tài)庫才能運(yùn)行。動態(tài)鏈接生成的程序體積較小,但是必須依賴所需的動態(tài)庫,否則無法執(zhí)行。
收集模塊準(zhǔn)備執(zhí)行的三個階段的規(guī)范名稱是鏈接-編輯(link-editing)、載入(loading)和運(yùn)行時鏈接(runtime linking)。靜態(tài)鏈接的模塊被鏈接編輯時載入,以便運(yùn)行。動態(tài)鏈接的模塊被鏈接編輯后載入,并在運(yùn)行時進(jìn)行鏈接以便運(yùn)行。
程序執(zhí)行時,在main函數(shù)被調(diào)用之前,運(yùn)行時載入器把共享的數(shù)據(jù)對象載入到進(jìn)程的地址空間。外部函數(shù)被真正調(diào)用之前,運(yùn)行時載入器并不解析它們。所以動態(tài)鏈接即使鏈接了函數(shù)庫,如果沒有實(shí)際調(diào)用,也不會帶來額外開銷。
gcc編譯器默認(rèn)使用動態(tài)鏈接:
gcc -o helloworld_shared helloworld.o
gcc編譯器使用靜態(tài)鏈接:
gcc -static -o helloworld_static helloworld.o
在windows平臺上使用mingw編譯發(fā)現(xiàn)這兩種方式生成的exe文件的大小都一樣,為40kb,懷疑mingw的編譯的庫是靜態(tài)庫。
下面看一下linux平臺上使用gcc兩種編譯方式的文件大小區(qū)別:
可以看到helloworld_shared的大小為8344,而helloworld_static的大小達(dá)到844792。
-nostartfiles:
不鏈接系統(tǒng)標(biāo)準(zhǔn)啟動文件,而標(biāo)準(zhǔn)庫文件仍然正常使用。
gcc -v -nostartfiles -o helloworld helloworld.oUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exeTarget: mingw32Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nlsThread model: win32gcc version 6.3.0 (MinGW.org GCC-6.3.0-1) COMPILER_PATH=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../libexec/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/LIBRARY_PATH=d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../lib/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../COLLECT_GCC_OPTIONS='-v' '-nostartfiles' '-o' 'helloworld.exe' '-mtune=generic' '-march=i586' d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:UsersMrBluAppDataLocalTempccZxAfxD.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -o helloworld.exe -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0 -Ld:/software/mingw/bin/../lib/gcc -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. helloworld.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrtertr000001.o:(.rdata+0x0): undefined reference to `_pei386_runtime_relocator'collect2.exe: error: ld returned 1 exit status
-nostdlib
不鏈接系統(tǒng)標(biāo)準(zhǔn)啟動文件和標(biāo)準(zhǔn)庫文件,會提示因為沒有鏈接系統(tǒng)標(biāo)準(zhǔn)啟動文件和標(biāo)準(zhǔn)庫文件,而鏈接失敗。
該選項常用于裸機(jī)/bootloader、linux內(nèi)核等程序,因為它們不需要啟動文件、標(biāo)準(zhǔn)庫文件。
gcc -v -nostdlib -o helloworld helloworld.oUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exeTarget: mingw32Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nlsThread model: win32gcc version 6.3.0 (MinGW.org GCC-6.3.0-1) COMPILER_PATH=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../libexec/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/LIBRARY_PATH=d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/;d:/software/mingw/bin/../lib/gcc/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib/;d:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../COLLECT_GCC_OPTIONS='-v' '-nostdlib' '-o' 'helloworld.exe' '-mtune=generic' '-march=i586' d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/collect2.exe -plugin d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/liblto_plugin-0.dll -plugin-opt=d:/software/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:UsersMrBluAppDataLocalTempccRAu2K4.res -Bdynamic -o helloworld.exe -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0 -Ld:/software/mingw/bin/../lib/gcc -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/lib -Ld:/software/mingw/bin/../lib/gcc/mingw32/6.3.0/../../.. helloworld.ohelloworld.o(.text+0xa): undefined reference to `__main'helloworld.o(.text+0x25): undefined reference to `puts'helloworld.o(.text+0x3b): undefined reference to `printf'helloworld.o(.text+0x47): undefined reference to `puts'collect2.exe: error: ld returned 1 exit status
動態(tài)鏈接的優(yōu)點(diǎn)
動態(tài)鏈接的優(yōu)點(diǎn)是可執(zhí)行文件的體積可以非常小。雖然運(yùn)行速度稍慢一些,但動態(tài)鏈接能夠更加有效的利用磁盤空間,而且鏈接-編輯階段的時間也會縮短(因為鏈接器的有些工作被推遲到載入時)。
動態(tài)鏈接的主要目的是把程序與它們使用的特定的函數(shù)庫版本中分離開來。取而代之的是,我們約定由系統(tǒng)向程序提供一個接口,該接口保持穩(wěn)定,不隨時間和操作系統(tǒng)的后續(xù)版本發(fā)生變化。
程序可以調(diào)用接口所承諾的服務(wù),而不必?fù)?dān)心這些功能是怎樣提供的或者它們的底層實(shí)現(xiàn)是否改變。由于它是介于應(yīng)用程序和函數(shù)庫二進(jìn)制可執(zhí)行文件所提供的服務(wù)之間的接口,所以稱它為應(yīng)用程序二進(jìn)制接口(Application Binary Interface,ABI)。
盡管單個可執(zhí)行文件的啟動速度稍受影響,但動態(tài)鏈接可以從兩個方面提高性能:
1.動態(tài)鏈接可執(zhí)行文件比功能相同的靜態(tài)鏈接可執(zhí)行文件的體積小。它能夠節(jié)省磁盤空間和虛擬內(nèi)存,因為函數(shù)庫只有在需要時才被映射到進(jìn)程中。
2.所有動態(tài)鏈接到某個特定函數(shù)庫的可執(zhí)行文件在運(yùn)行時共享該函數(shù)庫的一個單獨(dú)拷貝。操作系統(tǒng)內(nèi)核保證映射到內(nèi)存中的函數(shù)庫可以被所有使用它們的進(jìn)程共享。這就提供了更好的I/O和交換空間利用率,節(jié)省了物理內(nèi)存,從而提高了系統(tǒng)的整體性能。如果可執(zhí)行文件是靜態(tài)鏈接的,每個文件都將擁有一份函數(shù)庫的拷貝,顯然極為浪費(fèi)。
動態(tài)鏈接使得函數(shù)庫的版本升級更為容易。新的函數(shù)庫可以隨時發(fā)布,只要安裝到系統(tǒng)中,舊的程序就能夠自動獲得新版本函數(shù)庫的優(yōu)點(diǎn)而無需重新鏈接。動態(tài)鏈接允許用戶在運(yùn)行時選擇需要執(zhí)行的函數(shù)庫。這就使為了提高速度或提高內(nèi)存使用效率或包含額外的調(diào)試信息而創(chuàng)建新版本的函數(shù)庫是完全可能的,用戶可以根據(jù)自己的喜好,在程序執(zhí)行時用一個庫文件取代另一個庫文件。
動態(tài)鏈接是一種“just-in-time(JIT)”鏈接,這意味著程序在運(yùn)行時必須能夠找到它們所需要的函數(shù)庫。鏈接器通過把庫文件名或路徑名植入可執(zhí)行文件中來做到這一點(diǎn)。這意味著,函數(shù)庫的路徑不能隨意移動。如果把程序鏈接到/usr/lib/libthread.so庫,那么就不能把該函數(shù)庫移動到其他目錄,除非在鏈接器中進(jìn)行特別說明。否則,當(dāng)程序調(diào)用該函數(shù)庫的函數(shù)時,就會在運(yùn)行時導(dǎo)致失敗。當(dāng)在一臺機(jī)器上編譯完程序后,把它拿到另一臺不同的機(jī)器上運(yùn)行時,也可能出現(xiàn)這種情況。執(zhí)行程序的機(jī)器必須具有該程序需要鏈接的函數(shù)庫,而且這些函數(shù)庫必須位于在鏈接器中所說明的目錄。對于標(biāo)準(zhǔn)系統(tǒng)函數(shù)庫而言,這并不成問題。
任何人都可以創(chuàng)建靜態(tài)或動態(tài)的函數(shù)庫。只需簡單的編譯一些不包含main函數(shù)的代碼,并把編譯所生的.o用正確的工具進(jìn)行處理。
使用gcc程序編譯的過程中的示例代碼在Ubuntu下使用gcc創(chuàng)建靜態(tài)和動態(tài)庫:
1.首先將helloworld代碼拆分開來:
分別為“helloworld.c”、“helloworld.h”、“main.c”,
helloworld.c內(nèi)容如下:
#include
helloworld.h內(nèi)容如下:
#ifndef HELLO_WORLD_H #define HELLO_WORLD_H void helloworld(void);#endif
main.c內(nèi)容如下:
#include "helloworld.h"int main(){ helloworld(); return 0; }
2.將helloworld.c生成動態(tài)鏈接庫:
gcc -fPIC -shared helloworld.c -o libhelloworld.so
-fPIC:表示編譯為位置獨(dú)立的代碼,不用此選項的話編譯后的代碼是位置相關(guān)的所以動態(tài)載入時是通過代碼拷貝的方式來滿足不同進(jìn)程的需要,而不能達(dá)到真正代碼段共享的目的。與位置無關(guān)的代碼表示用這種方法產(chǎn)生的代碼保證對于任何全局?jǐn)?shù)據(jù)的訪問都是通過額外的間接方法完成的。這使它很容易對數(shù)據(jù)進(jìn)行重新定位,只要簡單的修改全局偏移量表的其中一個值就可以了。類似的,每個函數(shù)調(diào)用的產(chǎn)生就像是通過過程鏈接表的某個間接地址所產(chǎn)生的一樣。這樣,文本可以很容易的重新定位到任何地方,只要修改一下偏移量表就可以了。所以當(dāng)代碼在運(yùn)行時被映射進(jìn)來時,運(yùn)行時鏈接器可以直接把它們放在任何空閑的地方,而代碼本身并不需要修改。
在缺省情況下,編譯器并不產(chǎn)生與位置無關(guān)的代碼,因為額外的指針解除引用操作將使程序在運(yùn)行時稍慢。然而,如果不使用與位置無關(guān)的代碼,所產(chǎn)生的代碼就會被對應(yīng)到固定的地址,這對于可執(zhí)行文件來說確實(shí)很好,但對于共享庫,速度卻要慢一點(diǎn),因為現(xiàn)在每個全局引用就不得不在運(yùn)行時通過修改頁面安排到固定的位置,這使得頁面無法共享。
運(yùn)行時鏈接器總能夠安排對頁面的引用。但是,使用位置無關(guān)代碼,任務(wù)被極大的簡化了。當(dāng)然需要權(quán)衡一下,位置無關(guān)代碼與由運(yùn)行時鏈接器安排代碼相比,速度是快了還是慢了。根據(jù)經(jīng)驗,對于函數(shù)庫應(yīng)該始終使用與位置無關(guān)代碼。對于共享庫,與位置無關(guān)的代碼顯得格外有用,因為每個使用共享庫的進(jìn)程一般都會把它映射到不同的虛擬地址(盡管共享庫同一份物理拷貝)。
一個相關(guān)的術(shù)語是“純代碼(pure code)”。純可執(zhí)行文件是只包含代碼(無靜態(tài)或初始化過的數(shù)據(jù))的文件。它之所以稱為“純”是因為它不必進(jìn)行修改就能被其他特定的進(jìn)程執(zhí)行。它從堆?;蛘咂渌ǚ羌儯┒我脭?shù)據(jù)。純代碼可以被共享。如果生成與位置無關(guān)代碼(意味著共享),你通常也希望它是純代碼。
3.編譯main時加入libhelloworld.so:
gcc helloworld.h main.c -L. -lhelloworld -o main
-L. : 標(biāo)記告訴gcc函數(shù)庫可能位于當(dāng)前目錄。
-l :后面加上動態(tài)鏈接庫,動態(tài)鏈接庫的名字前的lib不用加上去。傳給C編譯器的命令行參數(shù)里并沒有提到函數(shù)庫的完整路徑名。它甚至沒有提到在函數(shù)庫目錄中該文件的完整名字。實(shí)際上,編譯器被告知根據(jù)選項-lname鏈接到相應(yīng)的函數(shù)庫,函數(shù)庫的名字是libname.so——換句話說,“l(fā)ib”部分和文件擴(kuò)展名被省略掉了,但在前面加了一個“l(fā)”。
4.運(yùn)行main,出錯:
./main: error while loading shared libraries: libhelloworld.so: cannot open shared object file: No such file or directory
程序在運(yùn)行時,會查找需要的動態(tài)庫文件,若找到,則載入動態(tài)庫,否則將提示類似上述錯誤而終止程序運(yùn)行。有多種方法可以解決:
a.將文件 libhelloworld.so復(fù)制到目錄/usr/lib中,再執(zhí)行則沒有問題:
mrbluyee@mrbluyee:~/mypro/C$ sudo mv libhelloworld.so /usr/libmrbluyee@mrbluyee:~/mypro/C$ ./maini = 0hello world
b.既然連接器會搜尋LD_LIBRARY_PATH所指定的目錄,那么我們可以將這個環(huán)境變量設(shè)置成當(dāng)前目錄:
mrbluyee@mrbluyee:~/mypro/C$ export LD_LIBRARY_PATH=$(pwd)mrbluyee@mrbluyee:~/mypro/C$ ./maini = 0helloworld
c. ldconfig命令
mrbluyee@mrbluyee:~/mypro/C$ sudo ldconfig ~/mypro/Cmrbluyee@mrbluyee:~/mypro/C$ ./maini = 0hello world
當(dāng)用戶在某個目錄下面創(chuàng)建或拷貝了一個動態(tài)鏈接庫,若想使其被系統(tǒng)共享,可以執(zhí)行一下”ldconfig 目錄名”這個命令。此命令的功能在于讓ldconfig將指定目錄下的動態(tài)鏈接庫被系統(tǒng)共享起來,意即:在緩存文件/etc/ld.so.cache中追加進(jìn)指定目錄下的共享庫。上述指令讓系統(tǒng)共享了~/mypro/C目錄下的動態(tài)鏈接庫。
可以查看程序執(zhí)行時調(diào)用動態(tài)庫的過程:
mrbluyee@mrbluyee:~/mypro/C$ ldd main linux-vdso.so.1 (0x00007ffd56fa4000) libhelloworld.so => /home/mrbluyee/mypro/C/libhelloworld.so (0x00007fcec730f000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcec6f1e000) /lib64/ld-linux-x86-64.so.2 (0x00007fcec7713000)
5.將helloworld.c生成靜態(tài)鏈接庫:
a. 先將helloworld.c編譯生成.o文件:
gcc -c -o helloworld.o helloworld.c
b.通過ar工具將目標(biāo)文件打包成.a靜態(tài)庫文件:
ar -rc libhelloworld.a helloworld.o
注意:靜態(tài)庫與匯編生成的目標(biāo)文件一起鏈接為可執(zhí)行文件,那么靜態(tài)庫必定跟.o文件格式相似。其實(shí)一個靜態(tài)庫可以簡單看成是一組目標(biāo)文件(.o/.obj文件)的集合,即很多目標(biāo)文件經(jīng)過壓縮打包后形成的一個文件。故ar工具里打包的一定是.o的文件,否則當(dāng)運(yùn)行連接了該靜態(tài)庫的可執(zhí)行程序會報錯。
6.編譯main時加入libhelloworld.a:
gcc helloworld.h main.c -static -L. -lhelloworld -o main_static
可以看到,使用動態(tài)鏈接庫生成的mian與靜態(tài)鏈接庫生成的main_static的大小區(qū)別:
動態(tài)鏈接庫生成的mian大小為8288,而靜態(tài)鏈接庫生成的main_static的大小為844856。
7.刪除libhelloworld.a后運(yùn)行main_static:
mrbluyee@mrbluyee:~/mypro/C$ rm libhelloworld.amrbluyee@mrbluyee:~/mypro/C$ ./main_static i = 0hello world
程序照常運(yùn)行,靜態(tài)庫中的helloworld函數(shù)已經(jīng)鏈接到main_static文件中了。
8.多個文件生成動態(tài)/靜態(tài)庫的用法:
動態(tài)庫:
1.gcc -fPIC -shared xxx1.c xxx2.c xxx3.c -o libxxx.so 2.gcc -fPIC -shared xxx1.o xxx2.o xxx3.o -o libxxx.so
靜態(tài)庫:
ar -rc libxxx.a xxx1.o xxx2.o xxx3.o
責(zé)任編輯:lq
-
模塊
+關(guān)注
關(guān)注
7文章
2787瀏覽量
50236 -
編譯器
+關(guān)注
關(guān)注
1文章
1661瀏覽量
50148 -
函數(shù)庫
+關(guān)注
關(guān)注
1文章
84瀏覽量
32700
原文標(biāo)題:GCC 程序編譯的靜態(tài)鏈接和動態(tài)鏈接
文章出處:【微信號:LinuxHub,微信公眾號:Linux愛好者】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
飛凌嵌入式ElfBoard ELF 1板卡-uboot編譯鏈接文件uboot.lds
AN-572:ADSP-219x上的疊加鏈接

深入探討Linux系統(tǒng)中的動態(tài)鏈接庫機(jī)制

分享關(guān)于編譯器的科普
安卓動態(tài)鏈接庫文件體積優(yōu)化探索實(shí)踐
深入解析Tricore的Tasking鏈接文件

三星貼片電容的引腳結(jié)構(gòu)和鏈接方式
KeyStone I和IlDevices上的SERDES鏈接調(diào)試

C2000 SysConfig 鏈接器命令工具數(shù)據(jù)手冊

先進(jìn)的鏈接器技術(shù),方便高效地使用內(nèi)存

評論