C++标头和 CPP 包括
快速提问。
我正在尝试确定 C++,今天我花了几个小时处理双重定义链接器错误(“这已经被定义了!”),我终于意识到这是因为我有这样的布局:
main.cpp< /p> <前><代码>#include Dog.cpp
Dog.cpp
<前><代码>#include Dog.hDog.h
//(Dog类和测试函数原型)
现在我已经通过在 main.cpp 中包含 Dog.h 而不是 Dog.cpp 来解决这个问题。
通过包含 .h 文件,具有相同前缀的 .cpp 文件是否会与程序一起编译?
当程序运行时仅包含 .h 并且没有任何对 Dog.cpp 的引用时,我感到很惊讶。我花了很长时间在谷歌上搜索,但没有任何答案真正帮助我理解发生了什么。
编辑:我忘记补充一点,我在 .h 中原型化,并在 .cpp 中定义了该类的函数,这就是给我带来“已定义”错误的原因。
quick question.
I am trying to get C++ nailed down, and today I spent hours with a double definition linker error("this has already been defined!") and I finally realised it's because I had the layout as such:
main.cpp
#include Dog.cpp
Dog.cpp
#include Dog.h
Dog.h
// (Dog class and prototype of test function)
And now that I've cleared that up by including the Dog.h instead of the Dog.cpp in the main.cpp.
By including the .h file, does the .cpp file with the identical prefix get compiled with the program?
I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp. I spent ages Googling but no answers really helped me understand what was going on.
Edit: I forgot to add that I prototyped in the .h, and defined the function for the class in the .cpp and that's what gave me the "already defined" error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不会。
您的计划是分阶段构建的。
对于编译阶段,每个翻译单元只需要声明(大致相当于带有
#include
的单个.cpp文件已解决)。声明首先存在的原因是作为一种“承诺”,稍后将找到完整的函数定义。对于链接阶段,符号在翻译单元之间解析。您必须将编译 Dog.cpp 和编译 main.cpp 的结果链接在一起(也许您的 IDE 正在为您执行此操作?),并且此链接过程会找到它们之间的所有正确函数定义以生成最终的可执行文件。
(要么,或者你实际上还没有进入链接阶段,并且只有一个目标文件(Dog.o);你无法执行它,部分原因是它没有所有函数定义in.)
这两个阶段可以同时完成,用“简写”:
No.
Your program is built in phases.
For the compilation phase, only declarations are needed in each translation unit (roughly equivalent to a single .cpp file with
#include
s resolved). The reason that declarations even exist in the first place is as a kind of "promise" that the full function definition will be found later.For the linking phase, symbols are resolved between translation units. You must be linking together the result of compiling Dog.cpp and of compiling main.cpp (perhaps your IDE is doing this for you?), and this link process finds all the correct function definitions between them to produce the final executable.
(Either that, or you actually haven't got to the link phase yet, and merely have an object file (Dog.o); you can't execute it, partially because it doesn't have all the function definitions in.)
The two phases can be done at the same time, with the "shorthand":
不,.cpp 文件不会自动编译。您可以手动执行此操作、创建 makefile,或者使用在同一项目中同时包含这两种文件的 IDE。
No, the .cpp file does NOT automatically get compiled. You can either do that manually, create a makefile, or use an IDE that has both of them in the same project.
您没有指定如何编译它。如果您使用 IDE 并且自动为项目添加了新的 .h 和 .cpp,那么它将自动编译和链接。
使可执行文件运行有两个阶段:编译和链接。编译是代码被解释并转换为较低级别代码的地方。链接是您使用的所有函数得到解析的地方。这就是您收到重复函数错误的地方。
You don't specify how you are compiling it. If you are using an IDE and have a new .h and .cpp to the project automatically then it will all be compiled and linked automatically.
There are 2 stages to making an executable to run: compiling and linking. Compiling is where the code gets interpretted and translated into lower level code. Linking is where all of the functions that you used get resolved. This is where you got the duplicate function error.
包含不会自动导致编译,不会。
事实上,实际的编译器根本看不到
#include
语句。它被前面的步骤(称为预处理器)删除。如果您从未编译过 Dog.cpp 文件,我不确定它是如何构建的。您是否使用该文件中定义的代码引用了任何对象?
Inclusion does not automatically cause compilation, no.
In fact, the actual compiler never sees the
#include
statement at all. It's removed by an earlier step (called the preprocessor).I'm not sure how it could build if you never compiled the
Dog.cpp
file. Did you reference any objects with code defined in that file?