问题描述
所以我完成了我的第一个C ++编程任务,并得到我的成绩。但根据分级,我失去了包括cpp文件的标记,而不是编译和链接它们
。我不太清楚这是什么意思。
So I finished my first C++ programming assignment and received my grade. But according to the grading, I lost marks for including cpp files instead of compiling and linking them
. I'm not too clear on what that means.
回顾我的代码,我选择不为我的类创建头文件,但做了一切cpp文件(它似乎工作正常没有头文件...)。我猜这个分级工意味着我在一些文件中写了#includemycppfile.cpp;'。
Taking a look back at my code, I chose not to create header files for my classes, but did everything in the cpp files (it seemed to work fine without header files...). I'm guessing that the grader meant that I wrote '#include "mycppfile.cpp";' in some of my files.
我对 #include
的推理是:cb文件是:
- 进入头文件是在我的cpp文件,所以我假装它就像一个头文件
- 在猴子看猴子做时尚,我看到其他头文件 #include
'd在文件中,所以我做了同样的我的cpp文件。
My reasoning for #include
'ing the cpp files was:- Everything that was supposed to go into the header file was in my cpp file, so I pretended it was like a header file- In monkey-see-monkey do fashion, I saw that other header files were #include
'd in the files, so I did the same for my cpp file.
那么什么是我做错了, ?
So what exactly did I do wrong, and why is it bad?
推荐答案
据我所知,C ++标准知道头文件和源文件没有区别。就语言而言,任何具有法律代码的文本文件都与任何其他文本文件相同。然而,虽然不是非法的,包括源文件到您的程序将几乎消除你将从分离您的源文件在第一位。
To the best of my knowledge, the C++ standard knows no difference between header files and source files. As far as the language is concerned, any text file with legal code is the same as any other. However, although not illegal, including source files into your program will pretty much eliminate any advantages you would've got from separating your source files in the first place.
基本上, #include
的作用是告诉预处理器接收您指定的整个文件,并将其复制到活动文件中编译器得到它的手。因此,当您将项目中的所有源文件包含在一起时,您所做的工作基本上没有差别,只需制作一个没有任何分离的大型源文件即可。
Essentially, what #include
does is tell the preprocessor to take the entire file you've specified, and copy it into your active file before the compiler gets its hands on it. So when you include all the source files in your project together, there is fundamentally no difference between what you've done, and just making one huge source file without any separation at all.
哦,这没什么大不了的,如果它运行,很好,我听到你哭了。在某种意义上,你会是正确的。但现在你正在处理一个微小的小程序,一个漂亮和相对不受支持的CPU来为你编译。
"Oh, that's no big deal. If it runs, it's fine," I hear you cry. And in a sense, you'd be correct. But right now you're dealing with a tiny tiny little program, and a nice and relatively unencumbered CPU to compile it for you. You won't always be so lucky.
如果你深入到严重的计算机编程领域,你会看到项目的行数可以达到数百万,而不是几十个。这是很多行。如果您尝试在现代台式电脑上编译其中一个,可能需要几个小时而不是几秒钟。
If you ever delve into the realms of serious computer programming, you'll be seeing projects with line counts that can reach millions, rather than dozens. That's a lot of lines. And if you try to compile one of these on a modern desktop computer, it can take a matter of hours instead of seconds.
哦,不!可怕的是,我可以防止这种可怕的命运吗?不幸的是,你可以做的不多。如果编译需要几个小时,编译需要几个小时。但是,只有第一次才真正重要 - 一旦你编译了一次,没有理由再编译它。
"Oh no! That sounds horrible! However can I prevent this dire fate?!" Unfortunately, there's not much you can do about that. If it takes hours to compile, it takes hours to compile. But that only really matters the first time -- once you've compiled it once, there's no reason to compile it again.
除非你改变一些东西。
现在,如果你有两百万行代码合并成一个巨型庞然大物,需要做一个简单的错误修复,例如, x = y + 1
,这意味着你必须再次编译所有两百万行以测试这个。如果你发现你打算做一个 x = y - 1
,然后再次,两百万行的编译等待你。
Now, if you had two million lines of code merged together into one giant behemoth, and need to do a simple bug fix such as, say, x = y + 1
, that means you have to compile all two million lines again in order to test this. And if you find out that you meant to do a x = y - 1
instead, then again, two million lines of compile are waiting for you. That's many hours of time wasted that could be better spent doing anything else.
但我讨厌没有生产力!如果只有一些方法可以编译单独的我的代码库的不同部分,并以某种方式之后链接他们在一起!理论上一个好主意。但是如果你的程序需要知道在不同的文件中发生了什么呢?除非你想运行一堆微小的.exe文件,否则不可能完全分离你的代码库。
"But I hate being unproductive! If only there was some way to compile distinct parts of my codebase individually, and somehow link them together afterwards!" An excellent idea, in theory. But what if your program needs to know what's going on in a different file? It's impossible to completely separate your codebase unless you want to run a bunch of tiny tiny .exe files instead.
但是肯定是可能的!像纯粹的折磨,如果我找到一些方法来分离接口从实现?通过从这些不同的代码段只需要足够的信息来识别他们的程序的其余部分,并把它们放在一些这样,我可以使用 #include
预处理器指令来引入 编译所需的信息!
"But surely it must be possible! Programming sounds like pure torture otherwise! What if I found some way to separate interface from implementation? Say by taking just enough information from these distinct code segments to identify them to the rest of the program, and putting them in some sort of header file instead? And that way, I can use the #include
preprocessor directive to bring in only the information necessary to compile!"
你可能会去那里的东西。让我知道如何为你工作。
Hmm. You might be on to something there. Let me know how that works out for you.
这篇关于为什么我不应该包括cpp文件而是使用头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!