Linux動態庫和靜態庫基礎

來源:互聯網
上載者:User

example: hello.c hello.h main.c

1.靜態庫
gcc -c hello.c
ar cr libhello.a hello.o
引用庫: -lhello

2.動態庫
gcc -c hello.c
gcc -shared -fPCI -o libhello.so hello.o
引用庫: -lhello

3.指定標頭檔
gcc main.c -o main -I include
include:標頭檔的存放路徑

4.指定庫檔案
gcc main.c -o main -L lib -lhello
lib:庫檔案的存放路徑

5.配置庫路徑
vi /etc/ld.so.conf
編輯,添加庫所在的路徑
# ldconfig 
重建ld.so.cache

6.配置庫環境變數
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib
離開終端就失效,可以寫入:~/.bashrc 或 ~/.bash_profile 或 /etc/profile

6.查看程式需要的動態庫
ldd ./main

6.gcc/g++ 常用選項
-Wall: 開啟幾乎所有的警告
-Werror:將所有的警告視為錯誤
-g:產生調試資訊
-O1/2/3:最佳化,有3個層級,一般使用-O2

一般為:-Wall -Werror -O2

7.【注意】:若目錄同時存在.a和.so庫,那編譯器預設會先使用.so動態庫,若要使用靜態庫,只需將.so重新命名或者移動到其他目錄即可。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.