調試linux的動態庫(動態庫的建立、調用、調試)__linux

來源:互聯網
上載者:User

add是庫lib.c中的函數,產生liblib.so

gcc lib.c -shared -g -DEBUG -o liblib.so

 

編譯調用庫的程式,指定庫檔案使用當前路徑

gcc applib.c -o applib -Wall -g liblib.so -Wl,-rpath,./

 

給動態庫函數add設定斷點

(gdb) b add

 

在下面提示中輸入 y

Function "add" not defined.

Make breakpoint pending on future shared library load? (y or [n]) y

 

Breakpoint 1 (add) pending.

 

 

執行程式 r

(gdb) r

 

提示如下:

Starting program: /home/zqk/work/webserver/applib 

 

Breakpoint 1, add (a=2, b=3) at lib.c:3

3 return a+b;

 

這時就進入到了動態庫的函數中。

 

繼續執行程式,輸入c

(gdb) c

Continuing.

Rusult is 5

 

Program exited normally.

 

 

以下是測試程式:

 

/*  lib.c */

int add(int a,int b)

{

return a+b;

}

 

/* lib.h */

extern int add(int a,int b);

 

/* applib */

#include <stdio.h>

#include "lib.h"

int main(void)

{

int a=2;

int b=3;

int result;

        result = add(a,b);

printf("Rusult is %d/n",result);

return 0;

}

相關文章

聯繫我們

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