關於動態庫的的編寫與使用過程

2021-09-22 10:09:02 字數 1490 閱讀 6986

什麼是動態庫

建立動態庫的方法(linux)

在linux下gcc編譯的執行檔案預設的elf格式,不需要初始化入口,也不需要對函式進行特別的宣告,直接編寫即可,十分方便。

例子:

1 #ifndef __math_h__

2 #define __math_h__

3 4 void add(int x, int y);

5 6 void sub(int x, int y);

7 8 void mul(int x, int y);

9 10 void div(int x, int y);

11 #endif

1 #include 2 #include "math.h"

3 4 void add(int x, int y)

5

編譯:

[root@localhost test_for_lib]# g++ test.cc -l./mathlib -lmath

[root@localhost test_for_lib]# ls

a.out mathlib test.cc

其中要注意的地方就是對於.so檔案在 -l的時候,要省略前邊的lib和後邊的.so欄位,比如庫名為 libmath.so 就寫 -lmath就ok了。

執行程式:

[root@localhost test_for_lib]# ./a.out 

./a.out: error while loading shared libraries: libmath.so: cannot open shared object file: no such file or directory

直接執行./a.out會報錯,因為系統在載入**的時候能知道所依賴的庫的名字,但是還需要知道絕對路徑,這時候就需要 系統動態載入器(dynamic linker/loader),對於elf格式的可執行程式,是由ld-linux.so*來完成的,他先搜尋elf檔案的dr_rpath段一環境變數ld_library_path-/etc/ld.so.cache檔案列表-/lib/ 或者 /usr/lib/ 下來找到庫檔案將其載入記憶體,所以當庫安裝在/usr/lib/ 或者 /lib/目錄下的時候ld預設能直接找到,不需要做其他的操作,若是將其安裝到其他的目錄地下需要編輯 /etc/ld.so.conf 檔案 include 庫的絕對路徑(到目錄層面即可),再執行 ldconfig 來重建 /etc/ld.so.cache檔案即可執行起來了

[root@localhost test_for_lib]# ldconfig

[root@localhost test_for_lib]# ./a.out

1 + 2 = 3

2 - 1 = 1

2 * 2 = 4

2 / 1 = 2

這樣就完成了乙個簡單的動態庫的編寫與使用過程

bro zeek的安裝與使用過程

該部落格主要的目的是將自己最近安裝的bro zeek進行整理,如有不對的地方希望大佬指正。其整理如下 首先,我的安裝環境是虛擬機器ubuntu16.4。bro zeek的安裝方式有兩種,一種是利用容器docker進行安裝,一種是單獨的安裝方式。我將首先講述第一種安裝方式。1.安裝docker,按照 ...

tiles的使用過程

本文以最簡單而有效的形式記錄tiles的使用過程,以備以後使用時快速參考。1.先建乙個layout.jsp布局頁面 2 新建乙個tiles defs.xml檔案,並在其中做相應的配置。這樣就相當於通過xml檔案定義了乙個member.jsp.3.在web.xml和struts config.xml和...

phpcms使用過程的總結

更換 網域名稱 需要替換 caches configs system.php phpsso server caches configs system.php 這兩個檔案裡頭跟網域名稱相關的資訊。模板編譯和解析 模板編譯和解析方法在template cache類中 phpcms libs classe...