linux下動態庫的建立與使用

來源:互聯網
上載者:User

標籤:內容   image   gre   lin   g++   cout   動態   設定   vim   

動態庫建立

 

1.編譯動態庫

Vim add.cpp

int add(int a, int b)

{

        return a + b;

}

Vim add.h

Int add(int a, int b);

 

g++ add.cpp -o libadd.so -fPIC -shared

 

2.連結動態庫

#include "add.h"

#include <iostream>

 

int main()

{

int ret = add(2, 3);

std::cout << ret << std::endl;

 

return 0;

}

 

g++ main.cpp -o main -I. -L. -ladd

 

3.設定動態庫載入路徑

編譯好程式後,運行,會出現問題,如下

 

我們需要把main程式運行需要的動態庫加入到系統載入動態庫路徑中

 centos系統中,在/etc/ld.so.conf中可以看到該系統的動態庫載入路徑

將libadd.so放到該路徑後,或者將libadd.so所在目錄加入到ld.so.conf中

運行ldconfig命令,更新系統動態庫緩衝

使用lddconfig -p|grep libadd.so  看一下該庫是否在動態庫緩衝中

 

一般動態庫載入到動態庫緩衝中,程式就可以調用到它了。

 

常用的關於動態庫的命令:

有時候動態庫會報找不到某個函數的錯誤,使用

ldd -r libadd.so 命令,查看libadd.so的依賴項,看看它是否有依賴項缺失

原創內容,轉載請註明出處http://www.cnblogs.com/abcdk/p/6719217.html

linux下動態庫的建立與使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.