我有麻烦试图编译此C++使用G++的程序?

发布于 2025-02-07 05:14:00 字数 461 浏览 2 评论 0原文

我正在使用MacOS操作系统&在终端中,我已经安装了G ++,当试图编译我的C ++程序文件时,该文件具有单独的汇编;发生以下错误消息:

main.cpp:3:10: fatal error: 'Item.h' file not found
#include "Item.h"
         ^~~~~~~~
1 error generated.

使用以下命令:g ++ main.cpp -std = C ++ 11

我假设我必须编译其他程序。一个是一个标题程序,该程序已声明为类,在另一个.CPP程序中,它在标题程序中定义了类。抱歉,如果这种解释写得不好,或者不理解,那么我正在遇到很大的麻烦,继续我的编程旅程,因此,如果您在这篇文章中无法理解一些内容,请对此帖子发表评论。谢谢,我会尽力向您解释这个主题。

I'm using a macOS operating system & within the terminal I have installed g++, when trying to compile my C++ Program File, which has separate compilation; the following error message occurs:

main.cpp:3:10: fatal error: 'Item.h' file not found
#include "Item.h"
         ^~~~~~~~
1 error generated.

with the following command: g++ main.cpp -std=c++11

I'm assuming I have to compile the other programs. One is a header program which has a declared class, and in another .cpp program, it defines the class in the header program. Sorry if this explanation is poorly written or not that understanding, I'm having big troubles continuing my programming journey so if there's something you can't understand in this post please leave a comment on this post & I will do my best to explain the subject to you, thanks.

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

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

发布评论

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

评论(1

能否归途做我良人 2025-02-14 05:14:00

如果item.h不在当前目录中,则需要将包含它的目录指定为g ++命令中的inclage目录。例如,如果您的文件夹结构是这样的:

.
├── headers
│   └── Item.h
├── items.cpp
└── main.cpp

那么您的G ++命令应

g++ -o main.o -std=c++11 -Iheaders -c main.cpp
g++ -o items.o -std=c++11 -Iheaders -c items.cpp
g++ -o program items.o main.o

注意,使用单独的翻译单元(CPP文件),您需要先将每个命令编译为对象文件(.o 文件,这就是-c选项所做的),然后将它们与最后一个命令链接在一起。

另外,使用少量的翻译单元,您可以在一个步骤中脱离编译并链接它们:

g++ -o program -Iheaders -std=c++11 main.cpp items.cpp

如果您有很多,我绝对建议设置makefile或其他某种其他形式的构建系统翻译单元。

If Item.h is not in your current directory, you will need to specify the directory containing it as an include directory in the g++ command. For example, if your folder structure is this:

.
├── headers
│   └── Item.h
├── items.cpp
└── main.cpp

Then your g++ command(s) should be

g++ -o main.o -std=c++11 -Iheaders -c main.cpp
g++ -o items.o -std=c++11 -Iheaders -c items.cpp
g++ -o program items.o main.o

Note that with separate translation units (cpp files), you need to compile each one to an object file first (.o file, that's what the -c option does), and then link them together with the last command.

Alternatively, with a small number of translation units, you can get away with compiling and linking them in one step:

g++ -o program -Iheaders -std=c++11 main.cpp items.cpp

I would definitely recommend setting up a Makefile or some other form of build system if you will have many translation units.

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