小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Unix/Linux C動(dòng)態(tài)庫(kù)的使用

 藍(lán)山逍遙屋 2011-09-13
Unix/Linux C動(dòng)態(tài)庫(kù)的使用
動(dòng)態(tài)庫(kù)的生成:
gcc -O -fpic -shared -o share.so share.c
有的gcc版本可以用"-G"替換"-shared"選項(xiàng).

eg.使用動(dòng)態(tài)庫(kù)
#cc -O test.c ./share.so

eg.帶路徑編譯
#cc -O test.c ./lib/share.so
當(dāng)執(zhí)行./a.out時(shí),操作系統(tǒng)會(huì)自動(dòng)在當(dāng)前目錄lib下查找share.so,若找不到程序?qū)⒈粴?
當(dāng)然也可以通過(guò)更改環(huán)境變量:
#LD_LIBRARY_PATH=./:share
#export LD_LIBRARY_PATH
#./a.out


例程:

dll1.c

#include <stdio.h>

int p = 1;

void print()
{
    printf("This is the first dll lib provided function: print().\n");

    return;
}

dll2.c

#include <stdio.h>

int p = 2;

void print()
{
    printf("This is the second dll lib provided function: print().\n");

    return;
}

implicit_test.c

int main (int argc, char *argv[])
{
    print();

    return 0;
}

explicit_test.c

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main(int argc, char *argv[])
{
    void *handle; /* pointer of dll lib handle */
    void (*pfunc)();
    int *p;

    handle = dlopen("./dll1.so", RTLD_NOW); /* RTLD_LAZY mode, parsed symbol
                         when called */

    p = (int *)dlsym(handle, "p");
    if (p != NULL)
    {
        printf("p = %d.\n", *p);
    }

    if ( dlsym(handle, "pp") == NULL )    /* none exist variable pp */
    {
        printf( "%s\n", dlerror() );        /* print error, and clean */
    }

    pfunc = (void (*)())dlsym(handle, "print");
    if (pfunc != NULL)
    {
        pfunc();
    }

    dlclose(handle);

    return 0;
}

Makefile

LIB= -ldl

dll:
    gcc -O -fpic -shared -o dll1.so dll1.c
    gcc -O -fpic -shared -o dll2.so dll2.c
    # some version of gcc supports "-G" instead of "-shared" option

implicit_test:
    gcc -O -o implicit_test implicit_test.c ./dll1.so
    # argument: ./dll1.so, explicitly tell the os to search dll location.
    # if not told path, when running, os will implicitly search at default
    # +LD_LIBRARY_PATH=./:dll/

explicit_test:
    gcc -O -o explicit_test $(LIB) explicit_test.c

clean:
    rm -f dll1.so dll2.so implicit_test explicit_test


 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多