Search code examples
c++visual-studiocompilationpreprocessor

C++ Conditional compilation directives: multiple files


I have 2 files,main.cpp and head.h

//main.cpp
#define DEBUG2019 1
#include 'head.h' 
int main{
A A1;
return 0;
}

//head.h
class A{
#ifdef DEBUG2019
int p;
#endif
int q;
};

Look, I have defined DEBUG2019 in main.cpp. But in my visual studio 2019, the int p is still greyed out in head.h. Why is that? Why head.h does not know that DEBUG2019 has been defined? You may suggest me to define DEBUG2019 in the header file directly. But I have to define it in main.cpp.


Solution

  • As far as I'm concerned, you should use #include "head.h" instead of #include 'head.h'

    Here is the code of main.cpp:

    #include <iostream>
    #define DEBUG2019 1
    #include "head.h"
    int main()
    {
    A A1;
    return 0;
    }
    

    enter image description here