Search code examples
cc-preprocessorpreprocessor

Standard include paths and defines in preprocessor


I'm gonna write a C preprocessor. Do I need to define the standard macros like __linux__ manually or are they defined somewhere I can just grab within the system?

Also, I've checked my environment variables but I can't find where the default include paths are. Do they need to be manually specified?

I've looked through all environment variables and some header files but there were no macros or standard include paths.


Solution

  • First of all, a C preprocessor is only required to predefine a few macros (C17, 6.10.8):

    __DATE__
    __FILE__
    __LINE__
    __STDC__
    __STDC_HOSTED__
    __STDC_VERSION__
    __TIME__
    

    Similarly, there are some specific macros that may be predefined (C17, 6.10.8.2 and 6.10.8.3).

    Anything beyond that is an additional service which you as the C preprocessor author may decide to offer your users, but of course you need to know how to define a given macro (hardcoded at compile-time, using a run-time detection mechanism, supplied by a configuration file, ...). There is no canonical way to do that.

    Standard include paths is also something that you need to define for your Preprocessor - whether they are provided in a configuration file, as program input, or you will interface with a specific compiler (e.g. GCC) or something else.

    Implementing a full C Preprocessor is not a small task. If using an existing implementation is an option, then that is probably to be preferred.