如何编译 C++文件组织在目录结构中?

发布于 2024-10-13 16:06:52 字数 364 浏览 7 评论 0原文

我现在已经编写了一堆代码,并以类似于此大纲的方式对其进行了排序:

project/
+ include/
| + bar/
| |   bar.hpp
|   foo.hpp
+ src/
| + bar/
| |   bar.cpp
|   foo.cpp
|   main.cpp

我的问题是,现在如何调用 g++ ,以便它将所有内容很好地链接在一起?

我已经发现我需要使用指向 include/ 目录的 -I 选项来调用它。我猜想从项目/文件夹中调用 g++ 是最有意义的。另外,我正在考虑编写一个 Makefile 来自动化此过程,但我必须承认我还没有对此进行太多研究。

I've written a bunch of code now, and sorted it in a fashion similar to this outline:

project/
+ include/
| + bar/
| |   bar.hpp
|   foo.hpp
+ src/
| + bar/
| |   bar.cpp
|   foo.cpp
|   main.cpp

My question is, how do I call g++ now, so that it links everything together nicely?

I've already figured out that I need to call it with the -I option pointing to the include/ directory. I'm guessing it would make most sense calling g++ from the project/ folder. Also, I'm considering writing a Makefile to automate this process, but I have to admit I haven't done very much research on that yet.

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

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

发布评论

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

评论(2

俏︾媚 2024-10-20 16:06:52

我建议使用某种构建工具,例如 CMake自动工具。创建自己的 Makefile 可能是一种正确的 PITA。

如果您有一个包含一些要快速编译的 C++ 文件的小目录结构,您可以执行以下操作:

find src/ -name "*.cpp" | xargs g++ -I include/

I would recommend using some kind of build tool like CMake or Autotools. Creating your own Makefiles can be kind of a PITA to get right.

If you have a small directory structure with some C++ files which you want to quickly compile, you can do something like this:

find src/ -name "*.cpp" | xargs g++ -I include/
农村范ル 2024-10-20 16:06:52

我认为最简单的方法是使用 IDE - 例如 NetBeans 将为您生成 Makefiles (其他 IDE 是 可用)。

I think the easiest approach is to use an IDE - for example NetBeans will generate Makefiles for you (other IDEs are available).

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