我已经搜索了一段时间,但是我一直得到的答案不能解决这个特定的情况。
我有一堂课叫做VisibleGameObject
。我想知道如果我将常规包含包含在标头中(以便其他开发人员可以使用相同的类),并且将相同包含包含在我的预编译标头中stdafx.h
会发生什么,我不想让开发人员依赖于我的pch。
// VisibleGameObject.h
#pragma once
#include "SFML\Graphics.hpp"
#include <string>
class VisibleGameObject
{
public:
VisibleGameObject();
virtual ~VisibleGameObject();
virtual void Load( std::string filename );
virtual void Draw( sf::RenderWindow & window );
virtual void SetPosition( float x, float y );
private:
sf::Sprite _sprite;
sf::Image _image;
std::string _filename;
bool _isLoaded;
};
实施:
// VisibleGameObject.cpp
#include "stdafx.h"
#include "VisibleGameObject.h"
...
PCH:
// stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
当我构建项目时(编译一次之后),#include <SFML/Graphics.hpp>
每次都会重新编译吗?因为它包含在此类的头文件中。我认为发生的情况是,pch首先包含在cpp文件的翻译单元中,然后#include <SFML/Graphics.hpp>
被包括保护,因此pch可以正常工作,而我的include被忽略了。在Visual Studio中,不首先包含pch是错误的。我只想确认这种行为。pch是否可以正常使用并且没有<SFML/Graphics.hpp>
重新编译代码?
如果标题的作者根本不加任何盐,则不会,它不会被重新编译。
PCH包含完整定义,包括#ifndef, #define, #endif
包含保护逻辑。在PCH创建期间,将拉入,编译文件,并正式定义包含保护标志。在您的源#include "stdax.h"
代码中将包含所有预编译的内容。源代码中将包含用于编译的可疑标头。但是,一旦#ifndef
找到了定义的包含保护的ID ,预处理器将跳过所有内容。注:这是可能的,可以重新编译为一个翻译单元,具体地PCH关断,但我怀疑你做到这一点。
简而言之,您做得正确,您的评估是准确的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句