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
Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
pighead10
  • 4,155
  • 10
  • 34
  • 52
  • What is in the `Framework.h`? Show the code. – Nawaz May 14 '11 at 17:34
  • 1
    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. – Boaz Yaniv May 14 '11 at 17:36
  • 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. – Boaz Yaniv May 14 '11 at 17:38
  • There's no precompiled header in here. See edit for Framework.h – pighead10 May 14 '11 at 18:21
  • 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? – Steve Folly May 14 '11 at 18:28
  • 2
    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: http://connect.microsoft.com/VisualStudio/feedback/details/651405/pch-warning-after-installing-sp1 – pighead10 May 14 '11 at 18:41
  • Er, right, now that the problem's solved, what should I do? Is there a way for me to close it as 'Solved'? – pighead10 May 14 '11 at 18:42

12 Answers12

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)

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
Kash
  • 1,118
  • 9
  • 10
  • That worked for me too, thanks. Any idea what the underlying problem is? – Mark Storer Feb 27 '12 at 23:34
  • 14
    @MarkStorer Following link from a previous post has the best explanation [http://connect.microsoft.com/VisualStudio/feedback/details/651405/pch-warning-after-installing-sp1](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. – Kash Mar 06 '12 at 17:53
  • 3
    @Kash link is dead. – kayleeFrye_onDeck May 22 '19 at 20:59
  • 1
    Upvoted, but I don't personally want to replace my include guards with `#pragma once`s. Then again I just closed and reopened the file and it solves the problem for me. – Marc.2377 Sep 11 '19 at 03:23
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".

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
7

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

One Double
  • 131
  • 2
  • 9
3

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

Nour
  • 47
  • 1
  • 2
  • 11
    Why we should do so? – Yola Jan 21 '15 at 07:19
  • 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. – p0358 Dec 04 '22 at 22:44
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
vahapt
  • 1,685
  • 1
  • 21
  • 26
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.

Fabian
  • 4,001
  • 4
  • 28
  • 59
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.

doug
  • 221
  • 3
  • 5
  • 12
1

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

Ilan
  • 11
  • 1
  • Yes, as noted in Kash's [comment](http://stackoverflow.com/questions/6003603/pch-warning-header-stop-cannot-be-in-a-macro-or-if-block-visual-c-2010-exp#comment12160887_6268238) above, this can fix the problem. – Chris Jones Mar 18 '15 at 23:12
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.

AndyG
  • 39,700
  • 8
  • 109
  • 143
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.

codyj110
  • 53
  • 1
  • 1
  • 6
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)=
George Valkov
  • 1,217
  • 12
  • 10
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.

p0358
  • 139
  • 1
  • 8