C++ 在一个 Eclipse 项目中构建多个二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2424795/
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
Building multiple binaries within one Eclipse project
提问by SF.
How can I get Eclipse to build many binaries at a time within one project (without writing a Makefile by hand)?
如何让 Eclipse 在一个项目中一次构建多个二进制文件(无需手动编写 Makefile)?
I have a CGI project that results in multiple .cgi programs to be run by the web server, plus several libraries used by them. The hand-made Makefile used to build it slowly becomes unmaintainable. We use Eclipse's "Internal Build" to build all other projects and we'd prefer to use it here too, but for the good of me, I can't find how to get Eclipse to build multiple small programs as result instead of linking everything into one binary.
我有一个 CGI 项目,该项目导致 Web 服务器运行多个 .cgi 程序,以及它们使用的几个库。用于构建它的手工 Makefile 慢慢变得不可维护。我们使用 Eclipse 的“内部构建”来构建所有其他项目,我们也更喜欢在这里使用它,但对我而言,我找不到如何让 Eclipse 构建多个小程序而不是链接所有内容成一个二进制。
回答by hovercraft
Solution for this described there: http://tinyguides.blogspot.ru/2013/04/multiple-binaries-in-single-eclipse-cdt.html. There is an excerpt:
那里描述的解决方案:http: //tinyguides.blogspot.ru/2013/04/multiple-binaries-in-single-eclipse-cdt.html。有一段摘录:
- Create a managed project (File > New C++ Project > Executable)
- Add the source code containing multiple main() functions
- Go to Project > Properties > C/C++ General > Path & Symbols > Manage Configurations
- Make a build configuration for each executable and name it appropriately (you can clone existing configurations like Debug and Release).
- From the project explorer, right click on each source file that contains a main() function > Resource Configurations > Exclude from Build and exclude all build configurations except the one that builds the executable with this main() function
- All other code is included in all build configurations by default. You may need to change this depending on your application.
- You can now build an executable for each main function by going to Project > Build Configurations > Set Active , Project > Build Project
- 创建托管项目(文件 > 新建 C++ 项目 > 可执行文件)
- 添加包含多个 main() 函数的源代码
- 转到项目 > 属性 > C/C++ 常规 > 路径和符号 > 管理配置
- 为每个可执行文件创建一个构建配置并为其命名(您可以克隆现有配置,例如 Debug 和 Release)。
- 在项目资源管理器中,右键单击包含 main() 函数的每个源文件 > 资源配置 > 从构建中排除并排除所有构建配置,除了使用此 main() 函数构建可执行文件的配置之外
- 默认情况下,所有其他代码都包含在所有构建配置中。您可能需要根据您的应用程序更改此设置。
- 您现在可以通过转到 Project > Build Configurations > Set Active , Project > Build Project 为每个主要功能构建一个可执行文件
回答by Joel Hoff
Using Eclipse as your build system for production code seems like a bad idea in general. I think it's a great IDE and have used it extensively for both Java and C++ projects, but for a build system I firmly believe that Ant, make, and other dedicated build utilities are the way to go.
使用 Eclipse 作为生产代码的构建系统一般来说似乎是一个坏主意。我认为它是一个很棒的 IDE,并且已经将它广泛用于 Java 和 C++ 项目,但是对于构建系统,我坚信 Ant、make 和其他专用构建实用程序是要走的路。
There are several reasons for this:
有几个原因:
Dedicated build utilities offer the very flexibility you are looking for in generating multiple executable targets.
Ant and make support most conceivable arbitrary build process chains (though not quite all).
A dedicated build utility is likely to offer greater stability and backward-compatibility for build description file formats than an IDE tool like Eclipse. Also, I'm pretty sure that Eclipse's internal build feature is dependent on the ".project" file description, and the latter's format is probably not as stable as the build description format for either Ant or make.
General-purpose, basic build utilities are usually command-line-based, which makes it easy to integrate them with more sophisticated, higher-level build utilities for automated build management like Pulse, CruiseControl, etc.
专用的构建实用程序在生成多个可执行目标时提供了您正在寻找的灵活性。
Ant 和 make 支持大多数可以想象的任意构建过程链(尽管不是全部)。
与 Eclipse 等 IDE 工具相比,专用的构建实用程序可能会为构建描述文件格式提供更高的稳定性和向后兼容性。另外,我很确定 Eclipse 的内部构建功能依赖于“.project”文件描述,而后者的格式可能不如 Ant 或 make 的构建描述格式稳定。
通用的基本构建实用程序通常基于命令行,这使得将它们与更复杂、更高级别的构建实用程序集成在一起以实现自动化构建管理(如 Pulse、CruiseControl 等)变得容易。
The need that is motivating your question is telling you that it's time to make the switch to a better build tool.
促使您提出问题的需求是告诉您是时候改用更好的构建工具了。
回答by Sven
There is a way to use buildconfigurations to create one binary (or shared library, in my case) from each build config. Using the answer above, this means to manually exclude all but the effective main file from each build config.
有一种方法可以使用buildconfiguration从每个构建配置创建一个二进制文件(或共享库,在我的情况下)。使用上面的答案,这意味着从每个构建配置中手动排除除有效主文件之外的所有文件。
I just used the above answers to ease up working on my eclipseproject that creates 14 shared libraries through 14 build configs. However, configuring the indivdual "exclude from build" setting was quite cumbersome, so I switched to using the following code relying on a preprocessor-directiveas my complete main file:
我只是使用上述答案来简化我的eclipse项目的工作,该项目通过 14 个构建配置创建了 14 个共享库。但是,配置单个“从构建中排除”设置非常麻烦,因此我转而使用以下依赖预处理器指令的代码作为我的完整主文件:
/*
*main.cpp
*/
/* Within
* Project | Properties | C/C++-Build | Settings
* | GCC C++ Compiler | Preprocessor
* set the following defined Symbol:
* _FILENAME=${ConfigName}
*/
#define __QUOT2__(x) #x
#define __QUOT1__(x) __QUOT2__(x)
#include __QUOT1__(_FILENAME.cpp)
#undef __QUOT1__
#undef __QUOT2__
/* The above include directive will include the file ${CfgName}.cpp,
* wherein ${CfgName} is the name of the build configuration currently
* active in the project.
*
* When right clicking in
* Project Tree | (Project)
* and selecting
* Build Configuration | Build all
* this file will include the corresponding .cpp file named after the
* build config and thereby effectively take that file as a main file.
*
* Remember to exclude ALL ${CfgName}.cpp files from ALL build configurations.
*/
Note that it does nothing else then include another .cpp file which's name is deduced from the preprocessorand a symbol that is set in the compiler options. The symbol is ${CfgName} and will be replaced by the current config name by eclipse automatically.
请注意,它不执行任何其他操作,然后包含另一个 .cpp 文件,该文件的名称是从预处理器和编译器选项中设置的符号推导出来的。符号是 ${CfgName} 并且会被 eclipse 自动替换为当前的配置名称。
One does not need to configure, which file is included in which build config. Just exclude all ${CfgName}.cpp files in every build and include main.cpp in every build.
一个不需要配置,哪个文件包含在哪个build config中。只需在每个构建中排除所有 ${CfgName}.cpp 文件,并在每个构建中包含 main.cpp。
PS: the answer from hovercraft gave me the idea to have a main file that does not contain code on its own. If one includes shared code from the different effective main files ${CfgName}.cpp, working on their code may become infeasible because header files in main.cpp will not be visible in them. I did this until yesterday, but maintaining the code with broken index etc. was a big pain.
PS:气垫船的答案给了我一个想法,即拥有一个不包含自身代码的主文件。如果包含来自不同有效主文件 ${CfgName}.cpp 的共享代码,则处理它们的代码可能变得不可行,因为 main.cpp 中的头文件在其中不可见。直到昨天我都这样做了,但是用损坏的索引等维护代码是一个很大的痛苦。
PPS: this procedure currently breaks the automatic rebuildof the main file if only the included .cpp file was changed. It seems that eclipse does not recognize the changes in ${CfgName}.cpp (which is excluded from build). So a manual rebuild is required after every change. This is currently bugging me ;)
PPS:如果仅更改了包含的 .cpp 文件,此过程当前会中断主文件的自动重建。eclipse 似乎无法识别 ${CfgName}.cpp (从构建中排除)中的更改。因此,每次更改后都需要手动重建。这目前困扰着我;)