• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計(jì)

            最新評(píng)論

            linux 中的動(dòng)態(tài)庫(kù)加載

            在Linux中可以動(dòng)態(tài)加載庫(kù),其使用方法如下:
            1. 先生成一個(gè)動(dòng)態(tài)庫(kù)libtest.so
            /* test.c */
            #include <stdio.h>
            #include <stdio.h>
            void test1(int no)
            {
                 printf("*****************************************\n");
                 printf("This is test1, the number is %d.\n", no);
                 printf("*****************************************\n");
            }
            void test2(char *str)
            {
                 printf("=========================================\n");
                 printf("This is test2, the string is %s.\n", str);
                 printf("=========================================\n");
            }

            編譯庫(kù):
            gcc -fPIC -shared -o libtest.so test.c
            這樣就可以生成libtest.so動(dòng)態(tài)庫(kù)。
            在這個(gè)庫(kù)里,定義個(gè)兩個(gè)函數(shù)test1,test2,下面將在程序中加載libtest.so,然后調(diào)用test1,test2。

            2. 動(dòng)態(tài)加載libtest.so
            /* main.c */
            #include <unistd.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <sys/types.h>
            #include <dlfcn.h> /* 必須加這個(gè)頭文件 */
            #include <assert.h>

            int main()
            {
                 void *handler = dlopen("./libtest.so", RTLD_NOW);
                 assert(handler != NULL);
                 void (*test1)(int) = dlsym(handler, "test1");
                 assert(test1 != NULL);
                 void (*test2)(char *) = dlsym(handler, "test2");
                 assert(test2 != NULL);
                 (*test1)(10);
                 (*test2)("hello");
                 dlclose(handler);
                 return 0;
            }
            /* end */
            在這個(gè)程序中,dlopen函數(shù)用來(lái)打開一個(gè)動(dòng)態(tài)庫(kù),其返回一個(gè)void *的指針,如果失敗,返回NULL。
            dlsym返回一個(gè)動(dòng)態(tài)庫(kù)中的一個(gè)函數(shù)指針,如果失敗,返回NULL。
            dlclose關(guān)閉指向動(dòng)態(tài)庫(kù)的指針。
            編譯的時(shí)候需要加上 -ldl
            gcc -o main main.c -ldl(編譯時(shí)要使用共享庫(kù)dl 其中有dlopen dlsynm dlerror dlclose 函數(shù))
            運(yùn)行main,將會(huì)看到調(diào)用test1,和test2的結(jié)果

            posted on 2009-06-02 22:56 大龍 閱讀(3945) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            亚洲精品无码久久久久久| 偷偷做久久久久网站| 99国产精品久久久久久久成人热| 好久久免费视频高清| 国产综合免费精品久久久| 中文字幕无码av激情不卡久久| 久久笫一福利免费导航| 99999久久久久久亚洲| 国产成人久久777777| 久久狠狠爱亚洲综合影院| 国产精品青草久久久久婷婷| 亚洲精品成人久久久| 久久伊人精品青青草原高清| 亚洲国产成人久久一区WWW| 99久久99这里只有免费的精品| 色偷偷91久久综合噜噜噜噜| 国产精品久久毛片完整版| 久久这里只有精品首页| 亚洲狠狠久久综合一区77777| 亚洲人成电影网站久久| 国产女人aaa级久久久级| 精品久久无码中文字幕| 国色天香久久久久久久小说| 久久久久国产| 88久久精品无码一区二区毛片| 国产亚洲综合久久系列| 亚洲中文字幕无码一久久区 | 香蕉久久夜色精品国产小说| 奇米影视7777久久精品人人爽| 中文字幕久久欲求不满| 久久精品国产99国产电影网 | 久久久久人妻精品一区| 国产99久久久国产精品小说| 九九久久精品无码专区| 精品国产乱码久久久久久浪潮| 久久国产精品久久| 久久精品国产亚洲AV无码娇色 | 色青青草原桃花久久综合| 久久精品国产亚洲Aⅴ蜜臀色欲 | 日产精品久久久久久久| 成人午夜精品无码区久久|