博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊gcc参数中的-I, -L和-l
阅读量:4094 次
发布时间:2019-05-25

本文共 1787 字,大约阅读时间需要 5 分钟。

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

  在本文中, 我们来聊聊gcc中三个常见的参数, 也即-I, -L和-l


        一. 先说 -I   (注意是大写的i)

        我们先来看简单的程序:

        main.c:

[cpp] 
 
  1. #include <stdio.h>  
  2. #include "add.h"  
  3.   
  4. int main()  
  5. {  
  6.     int a = 1;  
  7.     int b = 2;  
  8.     int c = add(a, b);  
  9.   
  10.     printf("sum is %d\n", c);  
  11.   
  12.     return 0;  
  13. }  
      add.c:
[cpp] 
 
  1. int add(int x, int y)  
  2. {  
  3.     return x + y;  
  4. }  
      add.h:
[cpp] 
 
  1. int add(int x, int y);<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>  

      编译链接运行如下:

[plain] 
 
  1. [taoge@localhost test]$ pwd  
  2. /home/taoge/test  
  3. [taoge@localhost test]$ ls  
  4. add.c  add.h  main.c  
  5. [taoge@localhost test]$ gcc main.c add.c  
  6. [taoge@localhost test]$ ./a.out   
  7. sum is 3  
  8. [taoge@localhost test]$   
       我们看到, 一切正常。 gcc会在程序当前目录、/usr/include和/usr/local/include目录下查找add.h文件, 刚好有, 所以ok.


       我们进行如下操作后再编译, 却发现有误, 不怕, 我们用-I就行了:

[plain] 
 
  1. [taoge@localhost test]$ ls  
  2. add.c  add.h  a.out  main.c  
  3. [taoge@localhost test]$ rm a.out; mkdir inc; mv add.h inc  
  4. [taoge@localhost test]$ ls  
  5. add.c  inc  main.c  
  6. [taoge@localhost test]$ gcc main.c add.c  
  7. main.c:2:17: error: add.h: No such file or directory  
  8. [taoge@localhost test]$   
  9. [taoge@localhost test]$   
  10. [taoge@localhost test]$   
  11. [taoge@localhost test]$ gcc -I ./inc/ main.c add.c   
  12. [taoge@localhost test]$ ls  
  13. add.c  a.out  inc  main.c  
  14. [taoge@localhost test]$ ./a.out   
  15. sum is 3  
  16. [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的作用就是用来指定具体的静态库、动态库是哪个。 

       请参考我之前的文章:


      

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


      

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


  

  

 

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


      

       OK, 都说完了, 希望对大家有所帮助。

       睡觉。




你可能感兴趣的文章
Win32程序之进程的原理
查看>>
C++虚函数原理
查看>>
MySQL的索引
查看>>
今天,Python信息量很大!
查看>>
Flash 已死,Deno 当立?
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>
程序员:凭自己能力吃饭,有什么理由瞧不起?
查看>>
面试想拿 10K,HR 说我只配7k?
查看>>
副业过万的程序员都知道的网站有哪些
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
乔布斯18岁求职信拍卖价22.24万美元,值吗?
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
假如计算机是中国人发明的,那代码应该这么写
查看>>
科技公司最爱的 50 款开源工具,你都用过吗?
查看>>
触目惊心:比特币到底消耗了多少能源?
查看>>
面试官:简历上敢写技术精通?那我就不客气了!
查看>>