• -l:表明需要一起编译的库文件。
  • -L:添加库文件所在的目录,只针对静态库。
  • -c:生成目标文件*.o
  • -o:指定输出文件。

静态库的使用方法

库文件名字为libxxx.a或者libxxx.so,使用时通过-l链接:

gcc -lxxx main.c

如果库文件不在/lib, /usr/lib/usr/local/lib三者中的任一个文件夹的话,要通过-L选项指定库所在的目录,否则会报错:/usr/bin/ld: cannot find -lxxx

> make
gcc main.c -lfunc1 -o debug/app # 没有制定库所在路径报错
/usr/bin/ld: cannot find -lfunc1
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target ''app'' failed
make: *** [app] Error 1
> vi Makefile # 修改Makefile添加-L选项
> make
gcc main.c -L. -lfunc1 -o debug/app # 编译成功
最后修改:2018 年 12 月 16 日
如果觉得我的文章对你有用,请随意赞赏