包括stdafx。h在头文件还是源文件?

时间:2022-10-11 02:10:11

I have a header file called stdafx.h and this one is precompiled of course. I've read that I should include these files into my .cpp files, but some of these statements are already needed in the header file coming with that.

我有一个头文件叫做stdafx。这个当然是预编译的。我已经读过,我应该将这些文件包含到我的.cpp文件中,但是在后面的头文件中已经需要这些语句。

Should I add the stdafx into my header or into my cpp? I thought it was good practise to put it into the header, but I seem to be obliged to put it into the header instead.

我应该将stdafx添加到标题中还是添加到cpp中?我认为把球打进头球是很好的练习,但我似乎不得不把球打进头球。

Example:

例子:

stdafx contains freeglut.
my class header file has an attribute of GLenum.

stdafx包含freeglut。我的类头文件有GLenum属性。

Should I include the stdafx into the .h of the class?

我应该将stdafx包含到这个类的。h中吗?

2 个解决方案

#1


22  

stdafx.h should be the first include in EVERY cpp file in your project.

stdafx。h应该是项目中每个cpp文件的第一个包含。


Consider that C++ doesn't compile header files, just Cpp files.

Therefore if the stdafx is the first include in the cpp file, then the compiler will have everything the header file needs, when it hits the headerfile in the Cpp file.

因此,如果stdafx是cpp文件中第一个包含的内容,那么当编译器碰到cpp文件中的headerfile时,它将拥有头文件所需的所有内容。

e.g.

如。

You have A.cpp & A.h.
A.h needs std:string.

你有一个。cpp & A.h。。h需要std:字符串。

you have B.cpp & B.h
B.h needs A.h therefore B.h needs std::string too.

你有B。cpp & B。B h。需要一个。因此,h B。h需要std::string。

Because it's good practise, you put #include <string> in stdafx.h.

因为这是很好的练习,所以在stdafx.h中输入#include

Your build fails because nothing can see std::string

您的构建失败了,因为没有人可以看到std::string

Now put stafx.h as the first include in A.cpp and B.cpp.
When the compiler hits A.cpp, it picks up the incldue for <string>, then picks up A.h, and everything is happy because we know what std::string is.

现在把stafx。h作为A中的第一个元素。cpp和B.cpp。当编译器遇到A。cpp,它选择了 ,然后拿起A。所有的一切都很快乐,因为我们知道std::string是什么。

The compiler now hits B.cpp, again it includes stdafx first, which brings <string>, then hits B.h, which brings A.h which is again happy because std::string has already been included.

编译器现在命中B。cpp,它首先包含stdafx,它带来 ,然后点击B。h,这带来了。h也很高兴,因为std::string已经包含在内了。

Hope this helps.

希望这个有帮助。

#2


6  

  • Only include things in your precompiled header which should be there
  • 只在预编译的标头中包含应该存在的内容
  • Your precompiled header file must be the first include in every .cpp
  • 您的预编译头文件必须是每个.cpp中的第一个
  • I would avoid including it in another header in favor of forward declaration
  • 我将避免将它包含在另一个页眉中,以便向前声明

Ask yourself these two questions before including something in stdafx.h

在写stdafx.h之前先问问自己这两个问题

  1. Will this header never be changed by me?
  2. 这个标题永远不会被我改变吗?
  3. Do I need this included in every multiple source file?
  4. 我需要在每个多源文件中包含这个吗?

If the answer is "No" to either of those then don't include it.

如果这两个问题的答案都是“不”,那就不要写了。

#1


22  

stdafx.h should be the first include in EVERY cpp file in your project.

stdafx。h应该是项目中每个cpp文件的第一个包含。


Consider that C++ doesn't compile header files, just Cpp files.

Therefore if the stdafx is the first include in the cpp file, then the compiler will have everything the header file needs, when it hits the headerfile in the Cpp file.

因此,如果stdafx是cpp文件中第一个包含的内容,那么当编译器碰到cpp文件中的headerfile时,它将拥有头文件所需的所有内容。

e.g.

如。

You have A.cpp & A.h.
A.h needs std:string.

你有一个。cpp & A.h。。h需要std:字符串。

you have B.cpp & B.h
B.h needs A.h therefore B.h needs std::string too.

你有B。cpp & B。B h。需要一个。因此,h B。h需要std::string。

Because it's good practise, you put #include <string> in stdafx.h.

因为这是很好的练习,所以在stdafx.h中输入#include

Your build fails because nothing can see std::string

您的构建失败了,因为没有人可以看到std::string

Now put stafx.h as the first include in A.cpp and B.cpp.
When the compiler hits A.cpp, it picks up the incldue for <string>, then picks up A.h, and everything is happy because we know what std::string is.

现在把stafx。h作为A中的第一个元素。cpp和B.cpp。当编译器遇到A。cpp,它选择了 ,然后拿起A。所有的一切都很快乐,因为我们知道std::string是什么。

The compiler now hits B.cpp, again it includes stdafx first, which brings <string>, then hits B.h, which brings A.h which is again happy because std::string has already been included.

编译器现在命中B。cpp,它首先包含stdafx,它带来 ,然后点击B。h,这带来了。h也很高兴,因为std::string已经包含在内了。

Hope this helps.

希望这个有帮助。

#2


6  

  • Only include things in your precompiled header which should be there
  • 只在预编译的标头中包含应该存在的内容
  • Your precompiled header file must be the first include in every .cpp
  • 您的预编译头文件必须是每个.cpp中的第一个
  • I would avoid including it in another header in favor of forward declaration
  • 我将避免将它包含在另一个页眉中,以便向前声明

Ask yourself these two questions before including something in stdafx.h

在写stdafx.h之前先问问自己这两个问题

  1. Will this header never be changed by me?
  2. 这个标题永远不会被我改变吗?
  3. Do I need this included in every multiple source file?
  4. 我需要在每个多源文件中包含这个吗?

If the answer is "No" to either of those then don't include it.

如果这两个问题的答案都是“不”,那就不要写了。