Eclipse CDT 基于文件构建/运行

发布于 2024-10-25 15:01:51 字数 374 浏览 1 评论 0原文

在我的场景中,我在 CDT Eclipse 中有一个 C++ 项目。然而,该项目更像是单个(帮助程序)程序的集合,而不是一个复杂的应用程序。因此,我希望能够单独构建和运行它们。

我的项目结构非常简单,如下所示:

src/app1.cpp
src/app2.cpp
src/...

请注意,我没有通用的头文件或库。但是,我希望能够通过创建例如 src/appx.cpp 来将程序添加到该项目中

理想情况下,我希望有

  • “构建当前打开的.cpp”
  • “运行当前打开的.cpp 的二进制文件”

的快捷方式有关如何实现的任何建议这种行为,如果可能的话,无需额外的插件?

In my scenario I have a C++ project in CDT Eclipse. This projects however is rather a collection of individual (helper) programs than one complex application. Consequently I want to be able to build and run them individually.

My project structure is very simple and looks like:

src/app1.cpp
src/app2.cpp
src/...

Note that I do not have common header files or libraries. However I want to be able to add programs to this project just by creating e.g. src/appx.cpp

Ideally I want to have shortcuts for

  • "Build currently opened .cpp"
  • "Run binary of currently opened .cpp"

Any suggestions on how to achieve this behaviour, if possible without additional plugins?

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

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

发布评论

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

评论(1

花心好男孩 2024-11-01 15:01:51

实现目标的直接方法是使用 CDT 创建 Makefile 项目,并为 Makefile 中的每个应用程序添加新的目标规则。您甚至可以将 SCons 或其他构建系统与 CDT Makefile 项目一起使用,并获得相同的效果。

您还可以欺骗托管构建来创建可执行文件而不是目标文件。从项目属性内 C++ 编译器设置的其他标志中删除 -c 选项。这将为每个源文件生成一个单独的应用程序文件。

在构建目录中创建的应用程序文件将具有目标文件扩展名,并且它们不可执行。要解决此问题,您可以在项目目录中添加一个构建后脚本,例如:

postbuild.sh(适用于 Linux):

 chmod +x *.o
 rename -v 's/\.o$//' *.o

postbuild.bat(适用于 Windows):

rename *.o *.exe

添加 之后../postbuild.sh../postbuild.bat 作为构建设置中的构建后命令,您的应用程序将准备好运行。右键单击任何这些可执行文件并选择调试为运行为,然后将创建一个新的运行配置。

此外,您还必须停止托管构建的链接器以防止错误。这可以通过将链接器命令更改为 true (Linux) 或 true.exe (Windows、msys) 来实现。

The straightforward way to succeed what you aim is to create a Makefile project with CDT and add a new target rule for each of your applications inside your Makefile. You can even use SCons or other build systems with a CDT Makefile project and obtain the same effect.

You can also trick the managed build to create executables instead of object files. Remove -c option from Other flags of C++ compiler settings inside project properties. This will produce a separate application file for each of your source files.

Application files which are created inside the build directory will have the object file extension and they will not be executable. To solve this, you can add a post build script in your project directory such as:

postbuild.sh for Linux:

 chmod +x *.o
 rename -v 's/\.o$//' *.o

or postbuild.bat for Windows:

rename *.o *.exe

After adding ../postbuild.sh or ../postbuild.bat as a post build command in your build settings, you applications will be ready to run. Right click on any of these executable files and choose Debug As or Run As and a new Run configuration will be created.

Also you will have to stop the linker of the managed build to prevent errors. This can be achieved with changing the linker command to true (Linux) or true.exe (Windows, msys).

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