PCH Warning: header stop cannot be in a macro or #if block - Visual C++ 2010 Express SP1
Asked Answered
M

12

71

This is pasted from a website, which presumably was working. I did some googling and found that the issue I have now is a result of Visual C++ 2010 SP1, which I downloaded today, and is now giving me this error:

PCH Warning: header stop cannot be in a macro or #if block.

Hopefully someone will be able to help me with this!

#ifndef APP_STATE_H
#define APP_STATE_H

#include "Framework.h"

class AppState; //this line is giving me the error

//define two classes

#endif

Framework.h:

#ifndef OGRE_FRAMEWORK_H
#define OGRE_FRAMEWORK_H

#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreOverlay.h>
#include <OgreOverlayElement.h>
#include <OgreOverlayManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>

#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>

class OgreFramework : public Ogre::Singleton<OgreFramework>,OIS::KeyListener,OIS::MouseListener{
public:
    OgreFramework();
    ~OgreFramework();

    bool initOgre(Ogre::String wndTitle, OIS::KeyListener *pKeyListener = 0, OIS::MouseListener *pMouseListener = 0);
    void updateOgre(double timeSinceLastFrame);

    //OIS
    bool keyPressed(const OIS::KeyEvent &keyEventRef);
    bool keyReleased(const OIS::KeyEvent &keyEventRef);
    bool mouseMoved(const OIS::MouseEvent &evt);
    bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id);
    bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id);

    Ogre::Root* mRoot;
    Ogre::RenderWindow* mRenderWnd;
    Ogre::Viewport* mViewport;
    Ogre::Log* mLog;
    Ogre::Timer* mTimer;

    //OIS
    OIS::InputManager* mInputMgr;
    OIS::Keyboard* mKeyboard;
    OIS::Mouse* mMouse;
private:
    OgreFramework(const OgreFramework&);
    OgreFramework& operator= (const OgreFramework&);
};

#endif
Misbehavior answered 14/5, 2011 at 17:32 Comment(7)
What is in the Framework.h? Show the code.Mendelian
Which file is that? And do you use Precompiled Headers? Because that's what PCH stands for. Do you have an StdAfx.h file? We probably need to see your whole include picture to understand where the error comes from. It seems like there's an uncloed #if block somewhere.Broadcaster
BTW, if Framework.h includes the Precompiled Header, that may very well be the error. Try to move its include outside the #if/#endif header guard (or use a #pragma once instead) and see what happens.Broadcaster
There's no precompiled header in here. See edit for Framework.hMisbehavior
Which file is being compiled? You've only shown two header files, but you must be compiling a cpp file? What are it's contents? There might be something before #include "AppState.h" (guessing the name here!) which is the cause of the error?Coffelt
There's no file which includes AppState.h here, otherwise I would've shown it! And from further Googling, it is a bug in Visual Studio but one that is resolved as soon as the problematic header if included in a cpp file, which is will be. If you're interested, here's someone who described the problem better than I and microsoft's response: connect.microsoft.com/VisualStudio/feedback/details/651405/…Misbehavior
Er, right, now that the problem's solved, what should I do? Is there a way for me to close it as 'Solved'?Misbehavior
A
94

I had the same issue and was looking for a solution. Following worked for me:

Add #pragma once at the start of the file (even before the #ifndef APP_STATE_H header guard)

Airstrip answered 7/6, 2011 at 16:7 Comment(4)
That worked for me too, thanks. Any idea what the underlying problem is?Dermatoid
@MarkStorer Following link from a previous post has the best explanation http://connect.microsoft.com/VisualStudio/feedback/details/651405/pch-warning-after-installing-sp1 - Intellisense engine was not happy about header files with a header stop and not included in any .c/.cpp files.Airstrip
@Airstrip link is dead.Subsidy
Upvoted, but I don't personally want to replace my include guards with #pragma onces. Then again I just closed and reopened the file and it solves the problem for me.Furtek
M
16

You probably used a project template to get started and threw away the pre-generated source code files. Those project templates like to turn on precompiled headers because it is such a time-saver. Right-click your project in the Solution Explorer window, Properties, C/C++, Precompiled Headers. Change the "Precompiled Header" setting to "Not Using".

Moy answered 14/5, 2011 at 18:40 Comment(1)
No, I didn't use that and there is no precompiled headers.Misbehavior
H
7

1.Close the Project. 2.Reopen the project,and all ok. this is my expeirence.

Honghonied answered 9/12, 2014 at 8:30 Comment(0)
S
3

move the #include statements outside the #if #end block

Saltcellar answered 7/7, 2014 at 9:12 Comment(2)
Why we should do so?Tjirebon
Correction: your PCH header needs to be moved outside of #if #end block to the top. Any other header can be inside, including others that include PCH header, since it was already included. Error probably comes from the fact some header that was included, includes the PCH header itself.Rubbish
M
3

Rebuilding IntelliSense database solves the problem.

  1. Close Visual Studio
  2. Delete [SolutionName].sdf
  3. Delete DllWrappers.opensdf
  4. Delete ipch folder
  5. Open Visual Studio
Marshallmarshallese answered 13/11, 2014 at 15:24 Comment(0)
B
3

I had the same problem. My solution was to add a missing ';' at the end of a class definition. Although this does not seem to apply to your problem, others who come here with the same error might find this helpful.

Biskra answered 17/4, 2015 at 4:48 Comment(0)
S
1

This is probable a day late and a dollar short but I had the same error when I accidentally put my header file in a .cpp file instead of a .h file. I will post it though in case it can help someone.

Subaudition answered 7/7, 2012 at 3:20 Comment(0)
B
1

I just added a referenct to the header file (#include "header.h") and it helped.

Bromley answered 23/6, 2014 at 6:51 Comment(1)
Yes, as noted in Kash's comment above, this can fix the problem.Bondmaid
C
1

I found that my .h file was actually being treated as a .cpp file! Right click on the file in the Solution Explorer > All Configurations > Item Type: C/C++ header

Make sure the item type is not C/C++ compiler or other.

Carbaugh answered 31/7, 2014 at 22:22 Comment(0)
H
0

Once you add a .cpp file and have it include the header this error should go away. I've read some where else this is a bug.

Hamamatsu answered 30/10, 2014 at 21:47 Comment(0)
P
0

I use Visual Studio to edit Linux projects. For me, the issue was present when I include string.h in my precompiled header file. It was caused by lines that have an __asm statement, for example:

__THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));

The solution was to define the following macro under Project Properties, Configuration Properties, C/C++, Preprocessor, Preprocessor Defines:

__asm(x)=
Petty answered 14/5, 2019 at 7:44 Comment(0)
R
0

In my case I was wrapping the whole .cpp file inside of #ifdef directive and getting this error.

The issue is, when using precompiled header, your standard precompiled header include needs to be outside of that!

For example:

#include "pch.h" // <- outside of any #if pragma directive

#ifdef EXAMPLE_DIRECTIVE

#include "another_header.h" // <- any other headers can be included inside

class AppState {}; // etc, your code here

#endif

Note that this issue can likely be also caused by some header you're including, which includes PCH header itself. If that's the case, just copy the import of PCH header from that file to the top of the file you're getting the error in, above the #if block.

Rubbish answered 4/12, 2022 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.