Search code examples
c++cvisual-c++header-files

How to enforce a header file at the forefront?


Motivation:

I want to enable the memory detection of VC++, which requires that some statements must be at the forefront as follows:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

Question:

Suppose I have a header file forefront.h, what I want is the following effect:

a.cpp

#include <any_other_one.h>
#include <forefront.h> // An compiler error generated here!

b.cpp

#include <forefront.h> // OK
#include <any_other_one.h>

How to implement?


Solution

  • Create your own header file with the following contents:

    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    

    Now use the Forced Includes setting in the Advanced section of the project's settings. Any file specified there will be included before all others, in the order specified.