linux系統中 庫分為靜態庫和,Linux系統靜態庫與共享庫

66b52468c121889b900d4956032f1009.png

8種機械鍵盤軸體對比

本人程序員,要買一個寫代碼的鍵盤,請問紅軸和茶軸怎么選?

This article mainly introduces the statics library and shared library on Linux and has done some experiments for better comprehension.

Static library,又稱為歸檔文檔(archive). 在Linux系統中一般以.a作為后綴名.用以聲明除了C語言標準庫之外的庫文檔的目錄. 這個聲明是靜態的,也就是說,當許多應用進程同時運行并且都是用來自同一個函數庫的函數時,在內存中就會存有這個函數的多份拷貝.這將大量消耗內存和磁盤空間. 類似與Windows中的靜態鏈接庫.lib文檔

共享庫(shared library / dynamic library)

共享庫克服了靜態庫的不足,典型的后綴名是.so。類似與Windows下的dll文檔。

In Arch Linux, the paths of shared library files are declared in /etc/ld.so.conf. You can add your specified path into this file and then using sudo ldconfig for generating their so-name files if there is update of these library files happening.

The naming suggestion of Linux shared library

Every shared library has its filename and so-name(Short for shared Object name, 簡單共享名). The following naming rules are commonly obeyed:

filename: libname.so.x.y.z

so-name: libname.so.x

x 代表了主版本號,主版本號之間不同通常是無法相互兼容的。

y 代表次版本號,可以向下兼容。

z 代表發布版本號,之間可以相互兼容。

當運行 ldconfig 命令后,系統會為制定目錄下面的動態庫文檔新建與 so-name 同名的軟鏈接。當編譯完進程需要鏈接的時候,查找的就是這些對應的 so-name。可以用環境變量 LD_LIBRARY_PATH 指定so-name files所在的目錄。

First experiment

Supposing that we want to create a shared library for calling function hello declared by hello.h, we start by writing our code here:

1

2

3

4

5

6

7void (const char* name)

{

printf("hello %s!n", name);

}1

2void (const char* name);

Then we compile it by gcc to generate shared lib:

1gcc hello.c -fPIC -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0.1

Let me explain every option of the above command. -fPIC means generating position independent code, i.e., address jumping is relative rather than absolute. This option is required in generating library file because lib file usually locates at some place and is called by programs from other places, or the program is generated at some place but is moved to other path. -shared -o indicates a shared library file .so.x.y.z. And -Wl,-soname,libhello.so.0 specifies its so-name as ‘libhello.so.0’.

Now we check our files and should see a new file like this picture:

Next we update by ldconfig

1ldconfig -n shared-library/

Note that -n specifies the dir only being processed(Because we only created one lib file under shared-library, it has no need to update all). If you have added the path into /etc/ld.so.conf, you can also simply run sudo ldconfig and see the same change:

As we can see, the so-name symbolic link has been created. Now we can test this new lib by writing a test code:

1

2

3

4

5

6

7

8int main()

{

hello("handy");

return 0;

}

Then we create a symbolic link to the so-name file in order for gcc compiler specification:

Now we make these three stages of shared library prepared(.so, .so.x and .so.x.y.z), then we compile and link, with relevent paths specified:

1gcc main.c -I /home/shane/Experiments/shared-library/ -L. -lhello -o main

where -I specifies the path of hello.h, -L for the path of libhello.so.

Since we have specified the path of so-name to gcc compiler but have not done that for Linux executer(one of the features of shared library), an error of failing to find the so-name file appears when running the program. So we use LD_LIBRARY_PATH to set it and run again:

1export LD_LIBRARY_PATH="$HOME/Experiments/shared-library/"

More exploration

用ldd查看其依賴的動態庫:

我們發現main進程依賴的動態庫名字是libhello.so.0,既不是libhello.so也不是libhello.so.0.0.1。其實在生成main進程的過程有如下幾步:鏈接器通過編譯命令-L. -lhello在當前目錄查找libhello.so文檔

讀取libhello.so鏈接指向的實際文檔,這里是libhello.so.0.0.1

讀取libhello.so.0.0.1中的SONAME,這里是libhello.so.0

將libhello.so.0記錄到main進程的二進制數據里

所以你看,進程并不知道 so-name file 在哪里,我們當然要在運行進程前 specify 一波了。

Second experiment

Now we emulate the situation of updating lib files. Suppose that we modify our code:

1

2

3

4

5

6

7# include

void hello(const char* name)

{

printf("hello %s, welcome to the world!n", name);

}

Since the change is trivial, we keep the so-name when compiling:

1gcc hello.c -fPIC -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0.2

Now there are two versions exist, we update by ldconfig and see the change:

The so-name file link to the new version of lib file. And we run the program and see the immediate change:

So you see, this is the significance or the essence of so-name mechanism. We don’t have to re-link the program after modifying the shared library code.

Summary

In practical production, the compilation and execution are usually departed. Generally:specify the so-name when generating shared lib files

Ensure the availability of libXXX.so file by -L and -l when linking executable program

Ensure the existence of shared lib file and use LD_LIBRARY_PATH to specify the directory of its so-name link when running program

References

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/259462.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259462.shtml
英文地址,請注明出處:http://en.pswp.cn/news/259462.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟件工程概論作業01

軟件工程作業01 寫一個能自動生成三十道小學四則運算題目的 “軟件”,要求:除了整數以外,還要支持真分數的四則運算(需要驗證結果的正確性)、題目避免重復、可定制出題的數量。 思路:隨機生成兩個數進行計算…

成員指針運算符 .* 和 -*

轉載: http://www.groad.net/bbs/thread-5548-1-1.html 有一種特殊的指針叫做成員指針,它們通常指向一個類的成員,而不是對象中成員的特定實例。 成員指針并不是真正的指針,它只是成員在對象中的偏移量,它們分別是&am…

捕捉Entity framework 6的詳細異常提示

采用 try{}catch (Exception e){throw;}不能捕捉到詳細異常提示, e.message的內容為"Validation failed for one or more entities. See EntityValidationErrors property for more details." 如果需要獲取詳細的異常提示,采用 1 try2 {3 return…

8.16——熟悉安裝linux系統

一、linux的版本——CentOS CentOS(Community ENTerprise Operating System)是Linux發行版之一,它是來自于Red Hat Enterprise Linux依照開放源代碼規定釋出的源代碼所編譯而成。由于出自同樣的源代碼,因此有些要求高度穩定性的服…

linux中設置默認權限的命令,Linux默認權限掩碼

Linux教程Linux教程:http://www.fdlly.com/m/linux文章目錄默認權限掩碼設置權限掩碼以文字的方式設置權限掩碼查看系統當前的權限掩碼默認權限掩碼當我們創建文件或目錄時,系統會自動根據權限掩碼來生成預設權限;默認情況下的umask值是022(可…

percona-toolkit工具包安裝

percona-toolkit工具包同percona-xtrabackup一樣都是用Perl寫的工具包,percona-toolkit工具包是一組高級的管理mysql的工具包集,可以用來執行各種通過手工執行非常復雜和麻煩的mysql和系統任務,在生產環境中能極大的提高效率,安裝…

C++允許重載的運算符和不允許重載的運算符

C中絕大部分的運算符允許重載&#xff0c;具體規定見表10.1。 表10.1 C允許重載的運算符雙目算術運算符 (加)&#xff0c;-(減)&#xff0c;*(乘)&#xff0c;/(除)&#xff0c;% (取模) 關系運算符 (等于)&#xff0c;! (不等于)&#xff0c;< (小于)&#xff0c;> (大…

Google Mesa概覽

Google Mesa的文章&#xff1a;https://research.google.com/pubs/pub42851.html https://gigaom.com/2014/08/07/google-shows-off-mesa-a-super-fast-data-warehouse-that-runs-across-data-centers/ 為什么未來的Hadoop是實時的&#xff1a; https://gigaom.com/2013/03/0…

C++數組參數應用方式探討(轉)

對于經驗豐富的編程人員來說&#xff0c;C編程語言應該是他們經常使用于程序開發的一種實用性語言。那么&#xff0c;在C中&#xff0c;C數組參數永遠不會按值傳遞。它是傳遞第一個元素&#xff08;準確地說是第0個&#xff09;的指針。 例如&#xff0c;如下聲明&#xff1a; …

一篇關于兼容問題的基礎總結

1.添加兼容文件(以 es5-shim 為例) 方法一&#xff1a; <script src"https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>在你的開發中&#xff0c;在需要為他做兼容的文件引入改文件 方法二(以模塊引入)&#xff1a; 在…

假如生活欺騙了你

假如生活欺騙了你&#xff0c; 不要悲傷&#xff0c;不要心急&#xff01; 憂郁的日子里需要鎮靜&#xff1a; 相信吧&#xff0c;快樂的日子將會降臨。 心兒永遠向往著未來&#xff1b; 現在卻常是憂郁&#xff0c; 一切都將會過去&#xff1b; 而那過去了的&#xff0c…

linux編譯mmc驅動,Embeded linux之MMC驅動

一、注冊平臺設備platform_device_register(&usr_mci_device);二、填寫平臺設備結構體static struct platform_device usr_mci_device {.name "xxx",.id 0,.dev {.release usr_mci_platdev_release,.dma_mask &usr_mmc_dmama…

redis windows下的環境搭建

先說下安裝吧&#xff01;感覺這東西跟mongodb差不多&#xff0c;安裝和布置挺簡單&#xff0c;下載地址&#xff1a;https://github.com/dmajkic/redis/downloads 下載下來的包里有兩個&#xff0c;一個是32位的&#xff0c;一個是64位的。根據自己的實情情況選擇&#xff0c;…

application/json 四種常見的 POST 提交數據方式

四種常見的 POST 提交數據方式 HTTP/1.1 協議規定的 HTTP 請求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 這幾種。其中 POST 一般用來向服務端提交數據&#xff0c;本文主要討論 POST 提交數據的幾種方式。 我們知道&#xff0c;HTTP 協議是以 ASCII 碼傳…

C++的4種類型轉換關鍵字及其特點

C中有四種類型轉換關鍵字&#xff0c;分別是reinterpret_cast,static_cast,const_cast,dynamic_cast.這是C 為了減少強制轉換的副作用&#xff0c;并且在查錯時使程序員能夠快速定位&#xff08;總是最值得懷疑的&#xff09;強制轉換&#xff0c;在標準C中新增加了4個關鍵字*…

linux系統數據庫類型,linux下的數據類型

sys/types.h sys/types.h中文名稱為基本系統數據類型。在應用程序源文件中包含 以訪問 _LP64 和 _ILP32 的定義。此頭文件還包含適當時應使用的多個基本派生類型。尤其是以下類型更為重要&#xff1a;caddr_t 核心地址。clock_t 表示系統時間(以時鐘周期為單位)。comp_t 壓縮的…

jsp亂碼

自從重裝系統之后電腦運行程序總是容易出現一些微妙的亂碼&#xff0c;一直都沒有徹底解決&#xff0c;有時候在別的機器上運行無誤的代碼一到我的機器上就出現一些問題。 myeclipse編碼方式怎么改都無效&#xff0c;每次只能再代碼中加上幾行轉碼的語句 今天終于找到罪魁禍首-…

如何使用Notepad++格式化XML文件

經常會從數據庫中讀到擠在一起的XML, 整理它們的格式需要使用一些工具. 比如筆者之前使用過online的tool. 后來經同事介紹, 改用VS2008的CtrlK, CtrlF來整理. 但是VS2008有點龐大, 開啟起來還是有點慢, 用起來也遠不如Notepad順手. 于是筆者Google了一把. 找到了下面的步驟, 非…

@MySQL的存儲引擎

1.存儲引擎 查看MySQL提供了哪些存儲引擎 mysql> show engines; ----------------------------------------------------------------------------------------------------------------------------- | Engine | Support | Comment …

聯想u盤linux安裝教程,聯想筆記本用U盤安裝 winXP系統教程

聯想筆記本用U盤安裝 winXP系統教程。聯想筆記本是指聯想集團生產的便攜手提電腦。 聯想集團成立于1984年&#xff0c;由中科院計算所投資20萬元人民幣、11名科技人員創辦&#xff0c;到今天已經發展成為一家在信息產業內多元化發展的大型企業集團。今天小編將給大家介紹使用U盤…