views:

48

answers:

2

Hello All,

If I define something in a pre-compiled header like:

#define __BUILD_MAC__

and then in a header file do:

#ifdef __BUILD_MAC__
    typedef void*   HINSTANCE;
#endif

This should work. But for some reason It isn't. I know because when I compile I get errors about HINSTANCE not naming a type.

Do I need to do anything else to make these defines available?

+1  A: 

Any defines before the pre-compiled header will be ignored. Consider doing your define via a compiler level switch or via:

Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions

You can read more on MSDN:

When you use a precompiled header, the compiler ignores all preprocessor directives (including pragmas) that appear before the hdrstoppragma. The compilation specified by such preprocessor directives must be the same as the compilation used to create the precompiled header file.

As for GCC pre compiled headers differ:

A precompiled header can't be used once the first C token is seen. You can have preprocessor directives before a precompiled header; you can even include a precompiled header from inside another header, so long as there are no C tokens before the #include.

Brian R. Bondy
A: 

Make sure __BUILD_MAC__ is #defined in the file actually being used to build the .pch, and before the last header being included in the .pch.

Ben Voigt
@Ben, yes, I do believe that was my mistake. I have includes, then defines and not I have define then includes.
ML
@Ben - To refine a little more, in a PCH if I #include <Carbon/Carbon.h> then I dont have to include that in header files where I use Carbon calls, correct? So if I get an error like: 'GetNewDialog' was not declared in this scope and the line of code is aDialog = GetNewDialog(16002, NULL,(WindowPtr) (-1L)); What would the issue be?
ML
Your code has to be written like the pch wasn't present. The best way to use header precompilation is to make a project-local header that `#include`s all the system headers you need for your project. Then `#include` that single header first in every .c or .cpp file. This will work with or without pch, but by enabling pch you'll make the compile go much faster because all those system files only get processed once.
Ben Voigt
reason for downvote?
Ben Voigt