1

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.

LinusEPK
  • 13
  • 3

2 Answers2

3

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.

nielsen
  • 5,641
  • 10
  • 27
-1

the standard macros like __linux__ are typically predefined by the compiler based on the target platform. You don't need to define them manually in your preprocessor :)

mtraceur
  • 3,254
  • 24
  • 33
  • 2
    The pre-processor *is* part of "the compiler" which pre-defines these things. (Note that the C pre-processor, when implemented separately from the compiler, runs before the compiler, and thus definitions which must be available in the pre-processor must come either from the pre-processor itself or from a system header.) – mtraceur May 22 '23 at 09:26