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”的程序构建.
原文链接:https://www.f2er.com/linux/439795.html每个.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/
在这些路径中,它首先搜索以“.so”扩展名命名的“动态库”.然后它搜索具有“.a”扩展名的静态库.它总是在名称的前面添加“lib”.