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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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


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

        我们先来看简单的程序:

        main.c:

#include <stdio.h>#include "add.h"int main()int a = 1int b = 2int c = add(a, b); printf("sum is %d\n", c); return 0;}
      add.c:
int add(int x, int y)return x + y;}
      add.h:
int add(int x, int y);<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>

      编译链接运行如下:

[taoge@localhost test]$ pwd/home/taoge/test[taoge@localhost test]$ lsadd.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就行了:

[taoge@localhost test]$ lsadd.c  add.h  a.out  main.c[taoge@localhost test]$ rm a.out; mkdir inc; mv add.h inc[taoge@localhost test]$ lsadd.c  inc  main.c[taoge@localhost test]$ gcc main.c add.cmain.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]$ lsadd.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, 都说完了, 希望对大家有所帮助。

       睡觉。










           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值