乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      GCC靜態(tài)鏈接與動(dòng)態(tài)鏈接 - 小呆學(xué)JAVA的日志 - 網(wǎng)易博客

       techres 2011-01-19

      GCC靜態(tài)鏈接與動(dòng)態(tài)鏈接

      Linux基礎(chǔ) 2009-08-14 13:35:44 閱讀460 評(píng)論0   字號(hào): 訂閱

      寫在前面:最近因?yàn)樾枰诓煌姹镜?/span>linux上運(yùn)行編譯后的文件,經(jīng)常會(huì)遇到找不到需要的鏈接庫文件的問題,后來突然想起了靜態(tài)編譯這一說。

      1:建靜態(tài)庫

      /*  hellos.h  */

      #ifndef _HELLO_S_H

      #define _HELLO_S_H

      void printS(char* str);

      #endif

      /*  hellos.c  */

      #include "hellos.h"

      void printS(char* str)

      {

          printf("print in static way: %s", str);

      }

      輸入命令:

      gcc -c -o hellos.o hellos.c

      ar cqs libhellos.a hellos.o      //ar是生成庫的命令,cqs是參數(shù), libhellos.a是生成的靜態(tài)鏈接庫須以lib開頭,hellos是庫名,a表示是靜態(tài)鏈接庫,hellos.o是剛才生成目標(biāo)文件。于是得到了libhellos.a這么一個(gè)靜態(tài)鏈接庫

       

      2:主程序

      /*  main.c  */

      #include <stdio.h>

      #include "hellos.h"

       

      main()

      {

          char* text = "Hello World!\n";

          printS(text);

      }

       

      編譯鏈接:

      gcc -o hello main.c -static -L. –lhellos

      下面是關(guān)于上面命令的解釋:

      庫依賴

      使用-I參數(shù)可以向gcc的頭文件搜索路徑中添加新目錄。

      gcc hello.c -I /home/wuzhiguo/include -o hello

      使用-L參數(shù)可以向gcc的庫文件搜索路徑中添加新目錄。

      gcc hello.c -L /home/wuzhiguo/lib -l mylib -o hello

      -l mylib 是指示gcc去鏈接庫文件libmylib.so。Linux下的庫文件有一個(gè)約定,全部以lib開頭,因此可以省去lib。

      動(dòng)態(tài)庫:.so結(jié)尾,在運(yùn)行時(shí)加載。

      靜態(tài)庫:.a結(jié)尾,在編譯時(shí)加載。

      默認(rèn)gcc優(yōu)先加載動(dòng)態(tài)庫,可以在通過-static選項(xiàng)強(qiáng)制使用靜態(tài)鏈接庫。

      gcc hello.c -L /home/wuzhiguo/lib -static -l mylib -o hello

             所以-L后面的點(diǎn)為當(dāng)前目錄,-l后面是要靜態(tài)連接的庫(libhellos.a

       

      然后運(yùn)行hello可以看到輸出

      print in static way: Hello World!

      刪除libhellos.ahellos.*, 程序仍然正常運(yùn)行。

       

      下面再來看動(dòng)態(tài)鏈接

      3:建動(dòng)態(tài)庫

      /*  hellod.h  */

      #ifndef _HELLO_D_H

      #define _HELLO_D_H

      void printD(char* str);

      #endif

      /*  hellod.c  */

      #include "hellod.h"

      void printD(char* str)

      {

          printf("print in dynamic way: %s", str);

      }

      輸入命令:

      gcc -shared -o libhellod.so hellod.c

      于是得到了libhellod.so這么一個(gè)動(dòng)態(tài)鏈接庫,然后復(fù)制到/lib目錄中,否則運(yùn)行的時(shí)候找不到庫文件。

       

      4:主程序

      /*  main.c  */

      #include <stdio.h>

      #include "hellod.h"

      main()

      {

          char* text = "Hello World!\n";

          printD(text);

      }

       

      編譯鏈接:

      gcc -o hello main.c -L. -lhellod

      然后運(yùn)行hello可以看到輸出

      print in dynamic way: Hello World!

      如果這時(shí)候刪除剛剛生成的hellod.dll,再運(yùn)行則會(huì)報(bào)告一個(gè)找不到hellod.dll的錯(cuò)誤,程序無法正常運(yùn)行。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(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)論公約

        類似文章 更多