• <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>

            大龍的博客

            常用鏈接

            統計

            最新評論

            linux 中的動態庫加載

            在Linux中可以動態加載庫,其使用方法如下:
            1. 先生成一個動態庫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");
            }

            編譯庫:
            gcc -fPIC -shared -o libtest.so test.c
            這樣就可以生成libtest.so動態庫。
            在這個庫里,定義個兩個函數test1,test2,下面將在程序中加載libtest.so,然后調用test1,test2。

            2. 動態加載libtest.so
            /* main.c */
            #include <unistd.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <sys/types.h>
            #include <dlfcn.h> /* 必須加這個頭文件 */
            #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 */
            在這個程序中,dlopen函數用來打開一個動態庫,其返回一個void *的指針,如果失敗,返回NULL。
            dlsym返回一個動態庫中的一個函數指針,如果失敗,返回NULL。
            dlclose關閉指向動態庫的指針。
            編譯的時候需要加上 -ldl
            gcc -o main main.c -ldl(編譯時要使用共享庫dl 其中有dlopen dlsynm dlerror dlclose 函數)
            運行main,將會看到調用test1,和test2的結果

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

            久久久久久av无码免费看大片| 武侠古典久久婷婷狼人伊人| 中文字幕精品久久久久人妻| 国产A三级久久精品| 思思久久99热只有频精品66| 蜜臀av性久久久久蜜臀aⅴ| 久久婷婷五月综合97色一本一本| 亚洲午夜久久久久妓女影院| 国产午夜精品理论片久久影视| 色婷婷久久久SWAG精品| 伊人久久综合无码成人网| 香蕉久久夜色精品国产小说| 精品久久久久久久国产潘金莲| 亚洲AV日韩AV天堂久久| 欧美午夜A∨大片久久| 久久综合给合久久狠狠狠97色| 大蕉久久伊人中文字幕| 美女写真久久影院| 国产精品久久久久久久久鸭 | 综合久久精品色| 国产精品久久久久久福利漫画 | 99精品伊人久久久大香线蕉| 国内精品久久久久| 亚洲日本va午夜中文字幕久久| 美女写真久久影院| 久久99国产精品久久久| 亚洲精品乱码久久久久久久久久久久 | 久久国产精品久久久| 久久精品国产亚洲AV久| 无码国产69精品久久久久网站| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 亚洲精品乱码久久久久久蜜桃图片 | 久久毛片免费看一区二区三区| 久久国产V一级毛多内射| 久久午夜综合久久| 国内精品伊人久久久久网站| WWW婷婷AV久久久影片| 婷婷综合久久中文字幕| 久久不射电影网| 99999久久久久久亚洲| 久久狠狠色狠狠色综合|