[Solved] What is the scope of pre-compiler define in c++?


test2.hpp and test.hpp both #include test1.hpp. If the scope of test1_hpp is the whole application, as far as I understand, there can only one include test1.hpp success. Because once included, test1_hpp is defined.

The compiler works on translation units (think: individual .cpp files) not the whole application (think: executable). What you call “the scope of the pre-compiler define” is the current translation unit. In your example, the // some declarations part in test1.hpp would be visible/processed in each of the CPPs that include test1.hpp directly or indirectly i.e. in all of test1.cpp (directly), test2.cpp, test3.cpp (indirectly, via both #include test1.hpp).

The #ifndef test1_hpp is a common idiom to prevent inadvertent inclusion of the same header file multiple times within the same translation unit – see for example “Use of #include guards” on Wikipedia.

2

solved What is the scope of pre-compiler define in c++?