头文件中的预编译头

头文件中的预编译头

本文介绍了头文件中的预编译头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次遇到了预编译的标题。以前改变了我的生活。我不能相信编译我的C ++代码可能是那么快。它现在是完全有意义的。

I ran into precompiled headers today for the first time..forever changing my life. I can't believe compiling my C++ code could be that fast. It makes total sense now..

无论如何,有一件事让我困惑的是,从我到目前为止,预编译的头只应该添加到源文件(cpp?)。

Anyway, one thing that is confusing me is that from what I've read so far, pre-compiled headers only should be added to source files( cpp? ).

在Visual Studio中,在项目属性 - > C / C ++ - >高级下有一个选项强制包含文件。我将编译器选项设置为 stdafx.h

In Visual Studio, there is an option under Project Properties->C/C++->Advanced to "Force Include File". I set that compiler option to stdafx.h.

这样做后,我不再需要包括我添加到 stdafx.h ,甚至在我的头文件(源文件应该自动包括stdafx.h)。这是预期的行为吗?

After doing this..I no longer require to include the headers I have added to my stdafx.h, even inside my header files ( source files are supposed to automatically include stdafx.h ). Is this expected behaviour?

我找不到标题/源文件之间区别的地方。

I can't find a place that's clear in the distinction between header/source files.

但是我恐怕这是另一个VC ++让你离开,但会打破GCC。是的,需要便携;至少在GCC和VC ++之间。

If it does..great but I'm afraid it's another one of those things VC++ lets you get away with but will break in GCC. And yes..it needs to be portable; at least between GCC and VC++.

推荐答案

StdAfx.h应该只包含在源文件中,而不是头文件中。我建议你#includeStdAfx.h首先在每个cpp和不使用强制包括文件选项。这是我如何做我的跨平台项目。对于记录,我实际上没有使用GCC中的预编译头文件,我只是正常构建它,它的工作原理。

StdAfx.h really should only be included in source files, not headers. I would suggest you #include "StdAfx.h" first in every cpp and not use the "Force Include File" option. Thats how I do it with my cross-platform projects. For the record, I don't actually use precompiled headers in GCC I just build it normally and it works well.

对于一些背景。编译器只查看源文件(即* .cpp,* .c等),因此当它编译它们时,它必须包括每个头,并编译在头中找到的任何代码。 precompiled headers选项允许一次编译所有的代码(即,StdAfx.h中的全局包含的代码),这样你就不必一直这么做。这是什么StdAfx.cpp是。编译器使用StdAfx.h中包含的所有代码编译StdAfx.cpp,而不是每次构建时都执行此操作。

For some background. The compiler only looks at source files (ie, *.cpp, *.c, etc) and so when it compiles them it has to include every header and compile any code found in the headers as well. The precompiled headers option allows for compiling all of that code (ie, the globally include'd code in StdAfx.h) once so that you don't have to do it all of the time. Thats what StdAfx.cpp is for. The compiler compiles StdAfx.cpp with all of the code included in StdAfx.h once instead of having to do it every time you build.

因此,由于您在每个源文件中都包含StdAfx.h作为第一项,因此将它包含在任何标题中没有意义,因为它们将包括在StdAfx.h之后,因此可以访问StdAfx.h中的所有代码。此外,您可以在其他项目中使用这些标题,而不必担心StdAfx.h周围或包括错误的。

So, since you include StdAfx.h in every source file as the first item, it doesn't make sense to include it in any of the headers since they will be included AFTER StdAfx.h and thus will have access to all of the code in StdAfx.h. Plus you can then use those headers in other projects without having to worry about having a StdAfx.h around or including the wrong one.

这篇关于头文件中的预编译头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:23