如何将多个源文件转换为一个 .a 文件

发布于 2024-11-10 11:19:02 字数 139 浏览 3 评论 0原文

我刚刚发现代码块(和 MingW 编译器)只接受 .a 库文件而不是 .lib 将 .lib 转换为 .a 文件的最简单方法...任何教程等将不胜感激。 编辑 让我稍微修改一下我的问题,如何将众多源文件转换为一个 .a 存档文件。

i have just found out that Code Blocks (and MingW compiler) only takes .a library files rather then .lib what is the easiest way to convert .lib to .a files... any tutorials etc would be appreciated.
Edit
let me modify my question slightly how can you convert numerous source files into one .a archive file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-11-17 11:19:02

回答具体问题,如何将多个源文件转换为单个归档文件以进行静态链接;这是一个两步操作。您必须首先将源文件编译为目标文件,然后将目标文件转换为存档。

如果您的 MinGW 安装中有 MSYS,我建议使用它。如果没有,您仍然可以使用 Windows 的命令提示符 cmd.exe。

确保您的 MinGW/bin 目录是 PATH 环境变量的一部分,以便您可以从任何地方调用编译器。

从命令行进入包含源代码的目录。从那里,键入命令

mingw32-gcc -O2 -c [files] -I[header directory]

您应该具体命名[files]-c a.cpp b.cpp c.cpp,或者您可以使用来识别它们>*.cpp[头目录] 是相对于您来说源的 .h 文件所在的位置。通常,源文件将位于一个名为 /src 的目录中,头文件位于名为 /include 的姊妹目录中。您可以将该目录称为 -I../include。如果头文件位于 /src 目录中名为 /include 的目录中,则它将是 -Iinclude

生成 .o 文件后,键入命令

ar rcs lib[something].a [files]

Replace [something] 为库的名称。这是将出现在 Code::Blocks 的“链接库”对话框中的名称。 Files 是您之前创建的目标文件的名称(ao、bo、co),或者,如果目录中没有不相关的目标文件,则可以放入 *.o

这应该会导致在目录中创建存档文件。现在,您可以将其放置在正确的目录中(可能是 /include 的姊妹目录,称为 /lib),并将该目录添加到您的 Code::Blocks 配置中的链接器搜索目录下。然后,您必须记住为您的项目实际添加库。

To answer the specific question, how to convert several source files into a single archive file for static linking; it is a two step operation. You must first compile the source files into object files, followed by turning the object files into an archive.

If you have MSYS with your MinGW installation, I recommend using that. If not, you can still use Windows' command prompt cmd.exe.

Make sure your MinGW/bin directory is part of the PATH environment variable so you can invoke the compiler from anywhere.

From the command line, move into the directory that holds the source code. From there, type the command

mingw32-gcc -O2 -c [files] -I[header directory]

You should either name the [files] specifically, -c a.cpp b.cpp c.cpp or you can identify them all with *.cpp. [header directory] is where the .h files for the source are, relative to you. Generally, source files will be in a directory by themselves called /src, and header files in a sister directory called /include. You would refer to that directory as -I../include. If the header files are in a directory called /include within the /src directory, it would be -Iinclude.

Once you have generated the .o files, you type the command

ar rcs lib[something].a [files]

Replace [something] with the name of the library. This is the name that will go in the Link Libraries dialog in Code::Blocks. Files is either the name of the object files you created before (a.o, b.o, c.o) or, if there are no unrelated object files in the directory, you can put in *.o.

That should result in the archive file being created within the directory. You can now place it in the proper directory (possibly a sister directory to /include called /lib), and add that directory to your Code::Blocks configuration, under Linker Search Directories. You must then remember to actually add the library for you project.

空‖城人不在 2024-11-17 11:19:02

MinGW 可以使用 .LIB 文件。以下链接指向使用 MS 编译器创建的 .LIB:

gcc b.c a.lib

在代码块中,您可以在对话框 Project|Build Options... 中添加库,然后转到 Linker Settings >s 选项卡并添加到链接库 框中。

MinGW can use .LIB files. The following links to a .LIB created with the MS compiler:

gcc b.c a.lib

In codeblocks, you add the library in the dialog Project|Build Options... and then go to the Linker Settings tab and add to the Link Libraries box.

猫烠⑼条掵仅有一顆心 2024-11-17 11:19:02

.lib(和 .a 也)在两种不同的功能下工作:

  • 作为程序所依赖的共享 Dll 的导入库。
  • 作为从源代码编译的一个或多个目标文件创建的存档库。

您在问题中并未真正指定您正在使用哪种表单。如果您希望采用第二种形式,则必须按照 Persson 的建议在 gcc 下将源代码构建为静态库,因为 msvc 工具使用的库格式不兼容。

但是,如果您使用第一种表单,则有一些好消息。 gcc 的 MinGW 端口应该能够与使用 msvc 开发工具创建的 coff 格式导入库透明地工作。您可以做的就是简单地将 *.lib 导入重命名为 lib*.a 并将其传递给链接器,就像任何其他 *.a 文件。链接器应该能够整理出其真实格式。

我已经在 tdm-gcc-4.5.2 下使用最新的每晚构建的代码块对此进行了测试,它似乎工作正常。

编辑:将我的评论合并为答案的一部分。
如果您使用第二种形式并且需要从源代码构建静态库,步骤大致如下:

如果存在现有的 msvc 项目,您可以通过将其导入到代码块中来将其用作起点。它将位于“文件”->“导入项目”->“Visual Studio”等下。

如果没有可用的文件,则从头开始创建一个新的 C::B 项目并不是太困难。只需转到文件->新建->项目,选择“静态库”作为项目类型,然后添加所需的任何源文件。如果该库是用半可移植的方式编写的,那么它的编译应该不会有太多麻烦。

The .lib (and .a also) works under two different capacities:

  • As an import library for a shared Dll that your program depends on.
  • As an archive library created from one or more object files compiled from source.

You didn't really specify in your question which form you're working with. If you're looking to do the second form, you'll have to build the source under gcc as a static library as suggested by Persson since the library format used by msvc tools aren't compatible.

However, there's some good news if you're working with the first form. The MinGW port of gcc should be able to work transparently with a coff-format import library created with msvc development tools. What you can do is simply rename the *.lib import into lib*.a and just pass that to the linker like it's any other *.a file. The linker should be able to sort out its real format.

I've tested this under tdm-gcc-4.5.2 with the latest nightly build of codeblocks and it appears to work ok.

Edit: Merged my comment as part of the answer.
If you're working with the second form and need to build a static library from source, the steps are roughly as follows:

If there's an existing msvc project you can use it as a starting point by importing it into codeblocks. It'll be under File->Import Project->Visual Studio etc.

If none is available making a new C::B project from scratch isn't too difficult. Just goto File->New->Project select 'Static Library' as the project type then add whatever source file is needed. If the library is written in a semi-portable matter it should compile without too much trouble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文