加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

C编译器如何发现-lm指向文件libm.a?

发布时间:2020-12-13 18:52:51 所属栏目:Linux 来源:网络整理
导读:什么是Linux中C编程的.a文件? 是库文件吗? To merge with the math library libm.a you would type cc -o program_name prog.c -lmwhen you compile the program. The -lm means: add in libm. If we wanted to add in the socket library libsocket.a to

什么是Linux中C编程的.a文件?
是库文件吗?

To merge with the math library libm.a you would type

 cc -o program_name prog.c -lm

when you compile the program. The -lm means: add in libm. If we wanted to add in the socket library libsocket.a to do some network programming as well,we would type

 cc -o program_name prog.c -lm -lsocket

and so on. 

这里编译器如何发现-lm指向文件libm.a,-lsocket指向libsocket.a?

如果我们将头文件添加到程序中,我们是否必须在编译时提及库?

最佳答案
正如Ignacio所说,.a文件是静态库. “a”代表“archive”,.a文件由名为“ar”的程序构建.

每个.a文件包含一个或多个.o文件和名称索引.在链接过程中,只有包含已使用名称的.o文件才会包含在最终程序中.这样就不会包含整个C库,只会复制像“printf”这样的函数.

编译器如何找到库?它有一个内置的库路径集合,可以搜索.举个例子,如果被问到,GCC会告诉你它的搜索路径:

# gcc -print-search-dirs
install: /usr/lib/gcc/i686-redhat-linux/4.4.4/
programs: =/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/:/usr/libexec/gcc/i686-redhat-linux/4.4.4/:/usr/libexec/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/bin/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/bin/
libraries: =/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/lib/i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../../i686-redhat-linux/lib/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../i686-redhat-linux/4.4.4/:/usr/lib/gcc/i686-redhat-linux/4.4.4/../../../:/lib/i686-redhat-linux/4.4.4/:/lib/:/usr/lib/i686-redhat-linux/4.4.4/:/usr/lib/

您可以使用“-L / path”选项添加更多库搜索路径.

在这些路径中,它首先搜索以“.so”扩展名命名的“动态库”.然后它搜索具有“.a”扩展名的静态库.它总是在名称的前面添加“lib”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读