C++ .o 文件与 .a 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/654713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.o files vs .a files
提问by D.Shawley
What is the difference between these two file types. I see that my C++ app links against both types during the construction of the executable.
这两种文件类型有什么区别。我看到我的 C++ 应用程序在构建可执行文件期间链接到这两种类型。
How to build .a files? links, references, and especially examples, are highly appreciated.
如何构建 .a 文件?链接、参考资料,尤其是示例,受到高度赞赏。
回答by D.Shawley
.o
files are objects. They are the output of the compiler and input to the linker/librarian.
.o
文件是对象。它们是编译器的输出和链接器/库管理器的输入。
.a
files are archives. They are groups of objects or static libraries and are also input into the linker.
.a
文件是档案。它们是一组对象或静态库,也被输入到链接器中。
Additional Content
附加内容
I didn't notice the "examples" part of your question. Generally you will be using a makefile to generate static libraries.
我没有注意到您问题的“示例”部分。通常,您将使用 makefile 来生成静态库。
AR = ar
CC = gcc
objects := hello.o world.o
libby.a: $(objects)
$(AR) rcu $@ $(objects)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
This will compile hello.c
and world.c
into objects and then archive them into library. Depending on the platform, you may also need to run a utility called ranlib
to generate the table of contents on the archive.
这将编译hello.c
并world.c
生成对象,然后将它们归档到库中。根据平台的不同,您可能还需要运行一个实用程序ranlib
来生成存档目录。
An interesting side note: .a
files are technically archive filesand not libraries. They are analogous to zip files without compression though they use a much older file format. The table of contents generated by utilities like ranlib
is what makes an archive a library. Java archive files (.jar
) are similar in that they are zip files that have some special directory structures created by the Java archiver.
一个有趣的旁注:.a
文件在技术上是存档文件而不是库。它们类似于没有压缩的 zip 文件,尽管它们使用更旧的文件格式。由实用程序生成的目录ranlib
是使存档成为库的原因。Java 归档文件 ( .jar
) 的相似之处在于它们是具有一些由 Java 归档程序创建的特殊目录结构的 zip 文件。
回答by Vatine
A .o file is the result of compiling a single compilation unit (essentially a source-code file, with associated header files) while a .a file is one or more .o files packaged up as a library.
.o 文件是编译单个编译单元(本质上是一个源代码文件,带有关联的头文件)的结果,而 .a 文件是一个或多个打包为库的 .o 文件。
回答by bstpierre
D Shawley's answer is good, I just wanted to add a couple of points because other answers reflect an incomplete understanding of what's going on.
D Shawley 的回答很好,我只想补充几点,因为其他答案反映了对正在发生的事情的不完整理解。
Keep in mind that archive files (.a) are not restricted to containing object files (.o). They may contain arbitrary files. Not often useful, but see dynamic linker dependenciy info embedded in an archivefor a stupid linker trick.
请记住,存档文件 (.a) 不限于包含目标文件 (.o)。它们可能包含任意文件。通常不太有用,但请参阅嵌入在存档中的动态链接器依赖信息,以了解愚蠢的链接器技巧。
Also notice that object files (.o) are not necessarily the result of a single compilation unit. It is possible to partially link several smaller object files into a single larger file.
另请注意,目标文件 (.o) 不一定是单个编译单元的结果。可以将几个较小的目标文件部分链接到一个较大的文件中。
http://www.mihaiu.name/2002/library_development_linux/-- search in this page for "partial"
http://www.mihaiu.name/2002/library_development_linux/-- 在此页面中搜索“部分”
回答by qrdl
You can use ar
to create .a
file (static library) from .o
files (object files)
您可以使用从文件(目标文件)ar
创建.a
文件(静态库.o
)
See man ar
for details.
详情请参阅man ar
。
回答by Dana Holt
I believe an .a file is an archive that can contain multiple object files.
我相信 .a 文件是一个可以包含多个目标文件的档案。