Search code examples
cgccc-preprocessorpragma

Show #pragma message in header only once during build


I have a bunch of #pragma message("A message")s in a config.h header that gets included in many places in the project; the goal is to inform which configuration options are chosen during build. The header is protected by #ifndef #define style headerguards. The problem is that every time this header gets included, the message is printed. Is there a better way to do this so that the message gets printed only once during the build?

Edit: I understand that build options are usually manipulated and viewed with build tools such as cmake, qmake, autotools etc. but I don't really have a choice on the build tools due to the nature of the project.


Solution

  • You could put the #pragma message in a separate file you only include once from config.h. gcc might print these pragmas even when inside a false conditional, but it won't include a file from inside a false conditional. So someting like this:

    /* config.h */
    #ifndef CONFIG_H
    #define CONFIG_H
    
    #ifndef CONFIG_MESSAGE_PRINTED
    #define CONFIG_MESSAGE_PRINTED
    #include "config_message.h"
    #endif
    
    /* ... */
    #endif /* CONFIG_H */
    

    In config_message.h:

    #pragma message("A message")