如何使用 Codelite 11.0.0 编译静态库? [英] How to compile a static library with Codelite 11.0.0?

查看:52
本文介绍了如何使用 Codelite 11.0.0 编译静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我为 C++ 项目编写的所有函数中编译一个静态库.我在 Ubuntu 16.04 上使用 CodeLite 11.0.0,配置为使用 GCC 作为编译器.

I would like to compile a static library out of all the functions I have written for a C++ project. I am using CodeLite 11.0.0 on Ubuntu 16.04, configured to use GCC as compiler.

我已按照 CodeLite 的教程 中的说明进行操作,根据该说明,这应该是可能的,并进行了更改项目类型从可执行文件到静态库.

I have followed the instructions in CodeLite's tutorial, according to which this should be possible, and changed the project type from Executable to Static Library.

运行项目(CTRL+F5 命令)后,我希望在 /Debug 文件夹中找到一个 .a 文件,与可执行文件.然而,我能找到的只有可执行文件和一些 .o.o.d 文件.当项目设置为可执行文件"时,我发现的情况与此相同.

After running the project (CTRL+F5 command), I expected to find a .a file in the /Debug folder, either along with, or in place of the executable file. All I could find, though, was the executable and a number of .o and .o.d files. The same I was finding when the project was set to Executable.

我尝试关闭并重新打开 CodeLite,但没有帮助.我找不到任何关于如何使用 CodeLite 构建静态库的官方/非官方示例.

I tried to close and reopen CodeLite, but it did not help. I can't find any official/unofficial example of how to build a static library with CodeLite.

有谁知道如何设置 CodeLite 来生成 .a 静态库文件?

Does anyone know how to set-up CodeLite to produce a .a static library file?

推荐答案

您可能已经发现,CodeLite 允许您更改设置 -> 常规 -> 项目类型的下拉菜单中的项目.

As you've probably discovered, CodeLite allows you to change the type of a project in the drop-down menu from Settings -> General -> Project type.

但是,这样做不会更改项目目标的名称.因此,如果你从一个可执行的 myprog 开始你的项目 - 例如,从中在项目文件夹下调试生成 ./Debug/myprog - 然后您将项目类型更改为静态库并重建它,调试build 仍会生成 ./Debug/myprog,但该文件现在实际上会是一个静态库,缺少惯用的 lib-前缀和 .a 扩展名.

Doing so, however, does not change the name of the project target. So, if you started off your project as an executable myprog - from which, say, the Debug build generated ./Debug/myprog under the project folder - then you change the project type to static library and rebuild it, the Debug build will still generate ./Debug/myprog, but that file will now in fact be a static library, lacking the customary lib-prefix and .a extension.

给输出文件一个常规的静态库名称 -libmyprog.a - 您需要返回 Settings -> General 和将输出文件从:

To give the output file a conventional static library name - libmyprog.a - you need to go back into Settings -> General and change Output File from:

$(IntermediateDirectory)/$(ProjectName)

到:

$(IntermediateDirectory)/lib$(ProjectName).a

然后重建项目,它将输出一个静态的目标图书馆看起来像一个.

Then rebuild the project and it will output a target that is a static library and looks like one.

当然,您必须对两者中的项目设置进行相同的更改DebugRelease 配置(如果您希望它们都生成)具有相同文件类型和文件名的目标.

Of course, you must make the same changes to the project settings in both the Debug and Release configurations if you want them both to produce targets with the same file type and file name.

不过……

如果这种将程序项目转换为静态库项目的方式不看起来很光滑,那可能是因为它是很少使用的转换.

If this way of converting a program project to a static library project does not seem very slick, that could be because it is a conversion of very little use.

转换后生成的静态库将包含完全相同的对象构建程序的文件,包括定义的目标文件原程序的main函数.让我们假设目标文件是 main.o,并且它定义了链接器可以看到的 0 个或多个其他函数.

The static library produced after the conversion will contain exactly the same object files that the program was built from, including the object file that defines the main function of the original program. Let's suppose that object file is main.o, and that it defines 0 or more other functions that the linker can see.

与静态库链接的任何其他程序 newprog 必须提供它自己的 main 函数位于不同的目标文件中,因此在任何此类链接中有两件事必须发生:-

Any other program, newprog, that is linked with the static library must provide its own main function in a different object file, so in any such linkage one of two things must happen:-

  • newprog的联动不需要libmyprog.a(main.o)中定义的任何函数,所以 libmyprog.a(main.o) 没有链接,也可能不存在.

  • The linkage of newprog does not need any function defined in libmyprog.a(main.o), so libmyprog.a(main.o) is not linked and might as well not exist.

newprog的链接确实需要一些函数,foo,定义在libmyprog.a(main.o),所以 libmyprog.a(main.o) 链接的;然后以及 foo 的定义,程序链接 main 的重复定义 - 它自己的定义加上libmyprog.a(main.o) 中的一个.重复定义是一个错误,因此链接失败.

The linkage of newprog does need some function, foo, defined in libmyprog.a(main.o), so libmyprog.a(main.o) is linked; then as well as the definition of foo, the program links duplicate definitions of main - its own definition plus the one in libmyprog.a(main.o). Duplicate definitions are an error, so the linkage fails.

将某个程序的 main 函数的定义放入静态成员中库是没有意义的,因为如果在另一个成员的链接中需要该成员程序,那么它的链接将失败.

Putting a definition of some program's main function into a member of a static library is pointless, because if that member is ever needed in the linkage of another program then its linkage will fail.

因此将您的程序项目转换为静态库项目需要一些转换前的重构:-

So converting your program project to a static library project calls for some refactoring prior to the conversion:-

  • 如果你想要在静态库中的任何函数在同一个源中定义file as main,然后你需要把它从那个源文件中取出并定义它是另一回事.

  • If any function that you want in the static library is defined in the same source file as main, then you need to take it out of that source file and define it is a different one.

之后,从项目中删除定义main的源文件.

After that, remove the source file that defines main from the project.

最后,转换并重建项目.

Lastly, convert and rebuild the project.

您必须进行重构才能从原始程序源代码中提取一堆适合构建成静态库的源文件.

You have to do that refactoring to extract from your original program source code a bunch of source files that are suitable for building into a static library.

假设您已经这样做了,使用以下命令创建静态库的简单方法CodeLite 将创建一个项目为此目的新建项目向导选择 Library -> Static Library 作为项目类型而不是某种类型的可执行文件.

Assuming you've done that, the straightforward way to create a static library with CodeLite is to create a project for that purpose and in the New Project Wizard choose Library -> Static Library as the project type instead of some kind of executable.

然后只需将新的或现有的源文件添加到静态库项目中直到它包含您希望库添加的所有函数的定义提供.构建、测试、调试、编辑……直到完成.

Then just add either new or existing source files to the static library project until it contains definitions of all the functions you want the library to provide. Build, test, debug, edit... until done.

这篇关于如何使用 Codelite 11.0.0 编译静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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