Linux 的動態函式庫副檔名通常為 .so,命名慣例為 libname.so。與靜態庫 .a 相比,動態庫能減少重複程式碼,並提升記憶體使用效率。
假設原始碼為 myfunction.c,可使用 gcc -shared -o libmyfunction.so myfunction.c 產生動態庫。若需要位置無關碼,可加入 -fPIC。
撰寫 test.c 呼叫庫函式後,可用 gcc -o test test.c /home/program/libmyfunction.so 連結指定路徑的動態庫,再執行 test 驗證。
若將 libmyfunction.so 複製到 /usr/lib,可用 gcc -o test test.c -lmyfunction 連結。此時編譯器會省略 lib 前綴與 .so 副檔名,並自動尋找對應函式庫。