如何做一个C编译器发现-lm所指向的文件libm.a? [英] How does a C compiler find that -lm is pointing to the file libm.a?

查看:1465
本文介绍了如何做一个C编译器发现-lm所指向的文件libm.a?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在linux下的C编程.a文件?
它是库文件?

What's .a files in C programming in linux ? Is it library file ?

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?

Here how compiler find that -lm is pointing to the file libm.a , and -lsocket as libsocket.a ?

如果我们的头文件添加到程序中,它是我们必须要提库在编译?

And if we add the header file to the program, Is it must we want to mention library while compiling ?

推荐答案

作为伊格纳西奥说,.a文件是静态库。在A代表档案和.a文件是由一个名为AR计划建造的。

As Ignacio says, .a files are static libraries. The "a" stands for "archive" and .a files are built by a program named "ar".

每个某文件包含一个或多个.o文件和名称的索引。在链接过程中只包含名字的使用.o文件纳入最后方案。这是这样,而不是整个C库,如printf上被复制只用功能。

Each .a file contains one or more .o files and an index of names. During the link process only the .o files that contain used names are included into the final program. This is so that instead of including the entire C library, only used functions like "printf" are copied.

编译器如何找到库?它具有所搜索库路径内置集合。作为一个例子,GCC会告诉你它的搜索路径,如果问:

How does the compiler find the libraries? It has a built-in collection of library paths that are searched. As an example, GCC will tell you its search paths if asked:

# 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 /路径选项添加更多的库搜索路径。

You can add more library search paths by using the "-L /path" option.

在这些路径,这对于其命名了。所以扩展动态库首先搜索。然后查找一个。一个扩展静态库。它总是将LIB的名称的前面。

In those paths, it first searches for "dynamic libraries" which are named with a ".so" extension. It then searches for static libraries with a ".a" extension. It always adds "lib" to the front of the name.

这篇关于如何做一个C编译器发现-lm所指向的文件libm.a?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆