如何编译 C++文件组织在目录结构中?
我现在已经编写了一堆代码,并以类似于此大纲的方式对其进行了排序:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用某种构建工具,例如 CMake 或 自动工具。创建自己的 Makefile 可能是一种正确的 PITA。
如果您有一个包含一些要快速编译的 C++ 文件的小目录结构,您可以执行以下操作:
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:
我认为最简单的方法是使用 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).