聊聊gcc引數中的 I, L和 l

2021-08-10 06:26:37 字數 2095 閱讀 6687

在本文中, 我們來聊聊gcc中三個常見的引數, 也即-i, -l和-l

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

我們先來看簡單的程式:

main.c:

[cpp]view plain

copy

#include 

#include "add.h"

int main()    

add.c:

[cpp]view plain

copy

int add(int x, int y)    

add.h:

[cpp]view plain

copy

int add(int x, int y);"font-family: arial, helvetica, sans-serif; background-color: rgb(255, 255, 255);"> 

[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的作用就是用來指定具體的靜態庫、動態庫是哪個。 

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

睡覺。

聊聊gcc引數中的 I L和 l

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!在本文中,我們來聊聊gcc中三個常見的引數,也即 i,l和 l 一.先說 i 注意是大寫的i 我們先來看簡單的程式 main.c include include add.h intmain add.c int add int x,int y add...

聊聊gcc引數中的 I L和 l

在本文中,我們來聊聊gcc中三個常見的引數,也即 i,l和 l 一.先說 i 注意是大寫的i 我們先來看簡單的程式 main.c include include add.h intmain add.c int add int x,int y add.h int add int x,int y fon...

聊聊gcc引數中的 I L和 l

在本文中,我們來聊聊gcc中三個常見的引數,也即 i,l和 l 一.先說 i 注意是大寫的i 我們先來看簡單的程式 main.c include include add.h intmain add.c int add int x,int y add.h int add int x,int y fon...