C++ 如何打开 .a 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2703629/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:24:13  来源:igfitidea点击:

How to open a .a file

c++filelinker.a

提问by Mgst

I've a little problem: I have to open a linker file that has .aextension. I use Dev-C++.

我有一个小问题:我必须打开一个带有.a扩展名的链接器文件。我使用 Dev-C++。

回答by el.pescado

.afiles are ararchives (something like zip archives) of object (.o) files. You can list files in .afile using arprogram:

.a文件是ar对象 ( .o) 文件的档案(类似于 zip 档案)。您可以.a使用ar程序列出文件中的文件:

ar t file.a

And extract all files:

并提取所有文件:

ar x file.a

回答by Donal Fellows

Files with the .aextension are static libraries using Unix file naming conventions. They're not much more than an indexed collection of object code. You don't so much open them (unless you've got a tool like nmor gdbavailable, both of which cando sensible things with a library if not necessarily what you might want) as tell the linker to use them when linking. With most linkers, it's important to put all libraries (both static and dynamic/shared) after your main program code on the linker command line and the order of libraries matters too.

带有.a扩展名的文件是使用 Unix 文件命名约定的静态库。它们只不过是目标代码的索引集合。您不必打开它们(除非您有类似nmgdb可用的工具,如果不一定是您想要的,它们都可以对库做明智的事情),而是告诉链接器在链接时使用它们。对于大多数链接器,将所有库(静态和动态/共享)放在链接器命令行上的主程序代码之后很重要,库的顺序也很重要。

回答by Clifford

Do you really mean that you want to openthe file, or rather that you wish to linkit with your code?

您的意思是真的要打开文件,还是希望其与代码链接

Dev-C++ by default is installed with the MinGW/GCC compiler. If the archive is not specifically built to work with MinGW (for example it may be a Cygwin or Linux archive), you will not be able to link it to MinGW generated code.

默认情况下,Dev-C++ 与 MinGW/GCC 编译器一起安装。如果存档不是专门为与 MinGW 一起使用而构建的(例如,它可能是 Cygwin 或 Linux 存档),您将无法将其链接到 MinGW 生成的代码。

If the archive is a MinGW/GCC compatible library, then you simply link it to your code. In Dev-C++ you need to add the archive to the project linker options, either by adding the full path to the archive (there's a button for that in the project options), or by placing the archive in a path defined by a -L<path>option, and then adding a -l<archive>option. Note that id the archive is called libXXX.a, then the -l<archive>option will be `-lXXX'; the "lib" prefix and ".a" extension are implicit.

如果存档是 MinGW/GCC 兼容库,那么您只需将其链接到您的代码。在 Dev-C++ 中,您需要将存档添加到项目链接器选项,或者通过添加到存档的完整路径(在项目选项中有一个按钮),或者通过将存档放置在由-L<path>选项定义的路径中,然后添加一个-l<archive>选项。请注意,归档文件的 id 称为 libXXX.a,则-l<archive>选项将是“-lXXX”;“lib”前缀和“.a”扩展名是隐含的。

If you simply want to inspect an archive to determine what external symbols it provides, then the nmutility can be used for that. If you want to extract the individual object files, then use the ar, though I cannot think of a good reason why you'd want to do either.

如果您只是想检查档案以确定它提供的外部符号,那么可以使用nm实用程序。如果您想提取单个对象文件,请使用ar,尽管我想不出您想要这样做的充分理由。

回答by KM?n

Try fstream, or fstream file_op("c:\\test.a",ios::in);, and dont forget to include fstream.h.

尝试fstreamfstream file_op("c:\\test.a",ios::in);,不要忘记包含fstream.h.