简体   繁体   English

包括.cpp的c ++:避免​​多个定义错误

[英]c++ including .cpp: avoiding multiple definition errors

I have a project that included a couple of files that are included in others 我有一个项目,其中包含几个其他文件

A.h --> B.h means A.h is included in B.h

There are a lot of inclusions like 有很多像

A.h --> X.h
B.h --> A.h
B.h --> X.h

Which all works fine. 一切正常。 Now I have the problem that I need to include a cpp file ( not my file, cant change anything with it ). 现在,我有一个问题,我需要包含一个cpp文件(不是我的文件,不能使用它进行任何更改)。 EDIT: There is no corresponding .h file for this .cpp As soon as I do something like 编辑:没有相应的.h文件为此.cpp一旦我做类似的事情

Z.cpp --> B.h

I get a compiler error for multiple declarations. 我收到多个声明的编译器错误。 I also tried 我也试过

Z.cpp --> B.cpp

Similar result, just less errors ( Bh is included in many other .h files ) 结果相似,错误更少(其他许多.h文件中都包含Bh)

How can I include this .cpp? 如何包含此.cpp?

EDIT: I do understand where the error is coming from, I just wonder if there is some workaround. 编辑:我确实知道错误是从哪里来的,我只是想知道是否有一些解决方法。

If you use QtCreator, then it should build the object files for each source file included in the project. 如果使用QtCreator,则它应为项目中包含的每个源文件构建目标文件。

That's why you get multiple definition errors. 这就是为什么您会遇到多个定义错误的原因。 You have, say, a source file Z.cpp which gets built into Zo . 例如,您有一个源文件Z.cpp ,它已内置到Zo Then you have Z.cpp included in B.cpp which gets built into Bo . 然后,您将Z.cpp包含的B.cpp内置到Bo Now when the linker tries to link Zo and Bo into the executable program, the symbols defined in Z.cpp will exist in both object files, leading to the errors. 现在,当链接器尝试将ZoBo链接到可执行程序时,在Z.cpp定义的符号将同时存在于两个目标文件中,从而导致错误。

The IDE (QtCreator) will make sure both source files are built and linked together, forming a single executable file. IDE(QtCreator)将确保两个源文件都已构建并链接在一起,形成一个可执行文件。 Don't #include any source file into another source file (or worse, header file). 不要#include任何源文件到另一个源文件(或更糟的是,头文件)中。

The classes, structures, function prototypes and external variable declarations are (or should be) in the header file that you include. 类,结构,函数原型和外部变量声明位于(或应该位于)所包含的文件中。 The implementation of the non-inline class or structure member functions and functions are in the source file, the compiler and linker will know how to reference them from the object files. 非内联类或结构成员函数的实现以及函数都在源文件中,编译器和链接器将知道如何从目标文件中引用它们。

This is basic stuff for C++ software development, I suggest you to read a book for an introduction on how to compile and build a program. 这是C ++软件开发的基本内容,建议您阅读一本书,以介绍如何编译和构建程序。

Basically, in C++, you #include *.h files in *.cpp source files, 基本上,在C ++中,您将* .h文件包含在* .cpp源文件中,
then you compile each *.cpp file, 然后编译每个* .cpp文件,
then link all the compiled files (usually *.o on *nix or *.obj on Windows) into an executable program, or a library to be then linked in a program. 然后所有编译文件(通常在* nix上为* .o,在Windows上为* .obj)链接到可执行程序或要链接到程序中的库。

I would consider really bad practice to #include a *,cpp file into another cpp file. 我认为将* .cpp文件包含到另一个cpp文件中确实是一种不好的做法。

As you are using QTCreator, you should have a project that already takes care of this process. 在使用QTCreator时,您应该有一个已经完成此过程的项目。

Given the comments here, I added my own header file. 给定这里的评论,我添加了自己的头文件。 that solved the problem. 解决了问题。 thanks for pointing this out. 感谢您指出了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM