Linux 上的存档文件在哪里?

Where is the archive files on Linux?

我正在通过一本书学习 C。书中提到的归档文件:

An archive contains .o files Ever used a .zip or a .tar file? Then you know how easy it is to create a file that contains other files. That’s exactly what a .a archive file is: a file containing other files. Open up a terminal or a command prompt and change into one of the library directories. These are the directories like /usr/lib or C:\MinGW\lib that contain the library code. In a library directory, you’ll find a whole bunch of .a archives. And there’s a command called nm that you can use to look inside them.

然而,当我查找那本书说的 lib 位置(在 Ubuntu 上)时,没有找到存档文件。 我怎样才能看到这些存档文件?

系统库的位置在不同的发行版中可能会略有不同。在 Ubuntu 上,您可以在 /usr/lib/x86_64-linux-gnu/usr/lib32 中分别找到 64 位和 32 位的静态库(事实上,这在旧的 Ubuntu 中略有不同)发行版。但在最近的发行版中 (>Ubuntu 12),这一直是一致的。

这取决于您安装的软件包。

例如,如果您安装 traceroute,那么您应该会在 /usr/lib/:

中看到类似这样的内容
# ls -l /usr/lib/*.a
-rw-r--r-- 1 root root 22448 Aug 29 12:45 /usr/lib/libsupp.a

您可以轻松制作自己的图书馆。例如:

mylib.c

int hello()
{
    return 1;
}

test.c

#include <stdio.h>

int hello();

int main()
{
    printf("Hello returned: %d\n", hello());
    return 0;
}

执行:

$ cc -c -o mylib.o mylib.c
$ ar r mylib.a mylib.o
$ cc -o test test.c mylib.a

$ ./test 
Hello returned: 1