Linux/Unix動態連結程式庫執行個體

來源:互聯網
上載者:User

使用nm查看檔案中的符號。
例如代碼:
cat sub1.c
-----------------------------------------------
#include <stdlib.h>

int prn1(char *str)
{
        printf("This is in sub1.d !!");
        printf("%s\n",str);
}
-----------------------------------------------
編譯命令:
$cc -c sub1.c    (產生sub1.o)
$nm sub1.o
00000000 t gcc2_compiled.
         U printf
00000000 T prn1
$ld -G -o sub1.d sub1.o
$nm sub1.d
00001210 A _DYNAMIC
00001204 A _GLOBAL_OFFSET_TABLE_
00001260 A __bss_start
00001260 A _edata
00001260 A _end
000001eb A _etext
000001c8 t gcc2_compiled.
         U printf
000001c8 T prn1

調用方代碼:
-----------------------------------------------
//#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <unistd.h>

#define __MAIN__
#include "Debug.h"
#undef __MAIN__

#define DEF_DL_FILE "sub1.d"
#define DEF_DL_FUN "prn1"

main(int argc,char *argv[])
{
        void *handle;
        int (*prn)(char *str);
        char *error;
        char dlFileName[100],dlFileDir[100],dlFunName[50];

        memset(dlFunName,'\0',50);
        memset(dlFileDir,'\0',100);
        memset(dlFileName,'\0',100);

        getcwd(dlFileDir,100);
        if ( argc >= 2 )
                sprintf(dlFileName,"%s/%s",dlFileDir,argv[1]);
        else
                sprintf(dlFileName,"%s/%s",dlFileDir,DEF_DL_FILE);
        MsgStr(dlFileName);

        if ( argc == 3 )
                strcpy(dlFunName,argv[2]);
        else
                strcpy(dlFunName,DEF_DL_FUN);
        MsgStr(dlFunName);

        handle=dlopen(dlFileName,RTLD_LAZY);
        if (!handle)
        {
                fputs (dlerror(), stderr);
                exit(1);
        }
        Msg("After dlopen()!!");

        prn = dlsym(handle, dlFunName);
        if ((error = dlerror()) != NULL)
        {
                fputs(error, stderr);
                exit(1);
        }
        Msg("After dlsym()!!");

        prn("Good!!");
        Msg("After prn()!!");
        dlclose(handle);
        Msg("After dlclose()!!");
}
-----------------------------------------------
編譯命令:
gcc -c main.c
gcc -o main.x main.o -ldl

-ldl啟用動態連結程式庫。

執行效果:
-----------------------------------------------
 :: dlFileName is string: /home/Mento/coding/c/dltst/sub1.d
 :: dlFunName is string: prn1
 :: Message: After dlopen()!!
 :: Message: After dlsym()!!
This is in sub1.d !!Good!!
 :: Message: After prn()!!
 :: Message: After dlclose()!!
-----------------------------------------------

相關文章

聯繫我們

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