0

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?

ML.
  • 195
  • 1
  • 1
  • 8
  • `__BUILD_MAC__` is a reserved identifier. You cannot have double-underscores, nor can a name start with an underscore followed by a capital. – GManNickG Apr 06 '10 at 00:27

2 Answers2

1

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
  • 339,232
  • 124
  • 596
  • 636
-1

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
  • 277,958
  • 43
  • 419
  • 720
  • @Ben, yes, I do believe that was my mistake. I have includes, then defines and not I have define then includes. – ML. Apr 05 '10 at 23:29
  • @Ben - To refine a little more, in a PCH if I #include 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. Apr 05 '10 at 23:37
  • 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 Apr 06 '10 at 03:18