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.

4
  • How can you "grab them within the system" if you don't even know which system you are dealing with? Commented May 22, 2023 at 9:32
  • The system will have to be specified so the program could have some path to a file on eg linux where the defines are. Commented May 22, 2023 at 9:37
  • So then you already know the system and can defined __linux__ etc accordingly. Commented May 22, 2023 at 9:39
  • Okay, so I have to define all the macros myself. Got it. Commented May 22, 2023 at 9:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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 :)

1 Comment

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.