日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

聊聊gcc参数中的-I, -L和-l

發布時間:2023/11/30 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 聊聊gcc参数中的-I, -L和-l 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/stpeace/article/details/49408665


? 在本文中, 我們來聊聊gcc中三個常見的參數, 也即-I, -L和-l


? ??? ? 一. 先說 -I ? (注意是大寫的i)

? ? ? ? 我們先來看簡單的程序:

? ? ? ? main.c:

[cpp]?view plain?copy
  • #include?<stdio.h>??
  • #include?"add.h"??
  • ??
  • int?main()??
  • {??
  • ????int?a?=?1;??
  • ????int?b?=?2;??
  • ????int?c?=?add(a,?b);??
  • ??
  • ????printf("sum?is?%d\n",?c);??
  • ??
  • ????return?0;??
  • }??
  • ? ? ? add.c: [cpp]?view plain?copy
  • int?add(int?x,?int?y)??
  • {??
  • ????return?x?+?y;??
  • }??
  • ? ? ? add.h: [cpp]?view plain?copy
  • int?add(int?x,?int?y);<span?style="font-family:?Arial,?Helvetica,?sans-serif;?background-color:?rgb(255,?255,?255);">?</span>??
  • ? ? ? 編譯鏈接運行如下:

    [plain]?view plain?copy
  • [taoge@localhost?test]$?pwd??
  • /home/taoge/test??
  • [taoge@localhost?test]$?ls??
  • add.c??add.h??main.c??
  • [taoge@localhost?test]$?gcc?main.c?add.c??
  • [taoge@localhost?test]$?./a.out???
  • sum?is?3??
  • [taoge@localhost?test]$???
  • ? ? ? ?我們看到, 一切正常。 gcc會在程序當前目錄、/usr/include和/usr/local/include目錄下查找add.h文件, 剛好有, 所以ok.


    ? ? ? ?我們進行如下操作后再編譯, 卻發現有誤, 不怕, 我們用-I就行了:

    [plain]?view plain?copy
  • [taoge@localhost?test]$?ls??
  • add.c??add.h??a.out??main.c??
  • [taoge@localhost?test]$?rm?a.out;?mkdir?inc;?mv?add.h?inc??
  • [taoge@localhost?test]$?ls??
  • add.c??inc??main.c??
  • [taoge@localhost?test]$?gcc?main.c?add.c??
  • main.c:2:17:?error:?add.h:?No?such?file?or?directory??
  • [taoge@localhost?test]$???
  • [taoge@localhost?test]$???
  • [taoge@localhost?test]$???
  • [taoge@localhost?test]$?gcc?-I?./inc/?main.c?add.c???
  • [taoge@localhost?test]$?ls??
  • add.c??a.out??inc??main.c??
  • [taoge@localhost?test]$?./a.out???
  • sum?is?3??
  • [taoge@localhost?test]$???
  • ? ? ? ?上面把add.h移動到inc目錄下后, gcc就找不到add.h了, 所以報錯。 此時,要利用-I來顯式指定頭文件的所在地, ?-I就是用來干這個的:告訴gcc去哪里找頭文件。



    ? ? ? ?二. 再來說-L(注意是大寫的L)

    ? ? ? ?我們上面已經說了, -I是用來告訴gcc去哪里找頭文件的, 那么-L實際上也很類似, 它是用來告訴gcc去哪里找庫文件。 通常來講, gcc默認會在程序當前目錄、/lib、/usr/lib和/usr/local/lib下找對應的庫。 -L的意思很明確了, 就不在贅述了。



    ? ? ? ?三. 最后說說-l (注意是小寫的L)

    ? ? ? ?我們之前討論過linux中的靜態庫和動態庫, -l的作用就是用來指定具體的靜態庫、動態庫是哪個。?

    ? ? ? ?請參考我之前的文章:


    ? ? ??如何在linux下寫靜態鏈接庫并賣給別人?

    ? ? ? http://blog.csdn.net/stpeace/article/details/47030017


    ? ? ?如何在linux下寫動態鏈接庫并賣給別人??

    ? ? ? http://blog.csdn.net/stpeace/article/details/47047679


    ??

    ??gcc編譯出現undefined reference to 'pthread_create'的解決方法

    ?

    ? ? ? ?http://blog.csdn.net/stpeace/article/details/43282611


    ? ? ??

    ? ? ? ?OK, 都說完了, 希望對大家有所幫助。

    ? ? ? ?睡覺。





    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的聊聊gcc参数中的-I, -L和-l的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。