简体   繁体   English

包含.cpp文件和.h文件(在cpp中具有相同内容)的区别?

[英]Difference in including the .cpp file and .h file (with the same content in cpp)?

I've recently started learning cpp from basics and was very much confused with the folowing: 我最近开始从基础知识中学习cpp,并且与下面的内容非常混淆:

Lets say I have a header( test.h which contains only declarations) with some content and some source file ( source.cpp ) and program produced some result. 让我说我有一个标题( test.h只包含声明)与一些内容和一些源文件( source.cpp )和程序产生一些结果。

If I have copied the same content of that header file to a .cpp file ( testcpp.cpp ) and included this in source.cpp 如果我已将该头文件的相同内容复制到.cpp文件( testcpp.cpp )并将其包含在source.cpp

In this case, I did not understood what difference it makes? 在这种情况下,我不明白它有什么不同?

( I'll not include this testcpp.cpp in make file ) 我不会在make文件中包含这个testcpp.cpp

I have seen some threads similar to this but couldn't get a clear idea!!! 我见过类似的一些线程,但无法得到一个清晰的想法!

I learnt the usage of header and cpp files and have used it correctly in projects till now, Please answer specific to this scenario (I know doing this way adds confusion but just want to know). 我学会了header和cpp文件的用法,并且在项目中使用它到现在为止,请回答具体到这个场景(我知道这样做会增加混乱但只是想知道)。 Will there be any difference doing so or it's just a common practice everyone follows ? 这样做会有什么不同吗,或者只是每个人都遵循的常见做法?

It changes nothing. 它什么都没改变。 It's just a convention whether you use a *.h or *.cpp or *.asdasd suffix, as long as it doesn't get compiled by itself. 这只是一个约定,你是否使用* .h或* .cpp或* .asdasd后缀,只要它不能自己编译。

Some projects use the .hxx extension for header files and .cc for source file. 有些项目对头文件使用.hxx扩展名,对源文件使用.cc扩展名。

Please, for the good of fellow programmers you'll work with, stick to common conventions and don't put header code in .cpp files. 请为了您的合作伙伴的利益,坚持常见的约定,不要将头文件代码放在.cpp文件中。

what difference it makes? 它有什么不同?

The extension of a header file has no effect on anything. 头文件的扩展名对任何内容都没有影响。 You could have just as well named the file test.mpg , .test or just test (changing the include directive obviously), and it would have worked just as well. 您可以将文件命名为test.mpg.test或仅test (显然更改include指令),它也可以正常工作。 The extension is for the benefit of the programmer, not the toolchain. 扩展是为了程序员的利益,而不是工具链。

However, it is a bad idea to name it anything other than .h, .hpp or whatever is your convention. 但是,将它命名为.h,.hpp以外的任何东西都是一个坏主意。 If you name it .mpg, people will think that it is a video, and not realising that it is a header file, try to play it in a media player. 如果你将其命名为.mpg,人们会认为它是一个视频,并没有意识到它是一个头文件,试着在媒体播放器中播放它。 If you name it .cpp, people will think that it is a source file and may attempt to compile it or maybe add definitions into it. 如果将其命名为.cpp,人们会认为它是源文件,可能会尝试编译它或者可能会在其中添加定义。

Including a file with the preprocessor is technically just copying contents of one file into another. 在预处理器中包含文件在技术上只是将一个文件的内容复制到另一个文件中。 Nothing more and nothing less. 没有更多,也没有更少。 Everything else about them is just convention. 关于他们的其他一切都只是惯例。

In makefile, when specifying source file, Can I give my source files with any extension(.fsfs, .xxx) rather than .cpp extension 在makefile中,指定源文件时,我可以为源文件提供任何扩展名(.fsfs,.xxx)而不是.cpp扩展名

Technically yes, however compilers usually use the source file extension to detect the language which they will fail to do in this case, so you would have to specify it explicitly. 技术上是的,但是编译器通常使用源文件扩展名来检测在这种情况下它们将无法执行的语言,因此您必须明确指定它。

#include just does a copy-n-paste of the file you include into the current file. #include只是将您包含的文件复制粘贴到当前文件中。 What the file is named doesn't matter one bit - you can name it "foo.exe" if you like; 该文件的名称与一位无关 - 如果您愿意,可以将其命名为“foo.exe”; as long as it contains valid source-code in the context where it is included all is well (but please don't use unconventional names, you'll just confuse people). 只要它包含在包含它的上下文中的有效源代码一切都很好(但请不要使用非常规名称,你只会混淆人们)。

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

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