According to the dragon book, one of the first steps the source code of a program goes through during compilation is the Preprocessor.

Assuming a language like C++ where there is no underlying virtual machine to abstract away the underlying platform, is there a way to deal with cross platform compilation without requiring the use of the Preprocessor?

Another use case would be code to remove code that should only be executed in "Debug Builds".

P.S: I'm not mentioning other uses of the preprocessor like defining a value for PI as these can easily be replaced with the use const global variables.

  • 2
    $\begingroup$ D seems to get along just fine without a text preprocessor, and it's pretty much feature-compatible with C++. Having said that, the one case that I think may trip you up is if you need to compile the same code base with different compilers for the same language. Even G++ and Clang aren't as compatible as you might think if you're doing truly advanced hackery. $\endgroup$
    – Pseudonym
    Jan 14 '14 at 6:10

Yes, it's entirely possible.

For example, you can define the language as requiring at least a minimal degree of dead-code elimination, so (for example) something like:

const bool debug = false;

if (debug)

...requires that not only is whatever() not called, but that it produce code that does not refer to whatever at all.

This gives roughly the same effect as something like:

#if debug

...would in C or C++. The only obvious difference is that by including the capability in the language, you typically require that the contents of the condition be syntactically correct for the target programming language (whereas C and C++ allow an #ifdef block to contain code that isn't syntactically correct C or C++, as long as the preprocessor can still detect the matching #endif).

Now, in some cases it's easy to require things that nobody knows how to do, thus producing a specification that nobody can implement. That would not be the case here though: compilers that can do the optimization above already exist.

Ada, for one example, includes a great deal of ability to tailor code to a specific platform without using a preprocessor like C and C++ do. In fact, it (Ada) arguably supports quite a bit more than C and C++ do in this regard.

  • $\begingroup$ Is there any requirement that even C actually use a preprocessor? My impression is that Borland's Turbo C product did not use a preprocessor, but instead did macro substitution and other things on the same compiler pass as initial code generation (thus making it 3x as fast as competing compilers). $\endgroup$
    – supercat
    Jan 30 '14 at 17:58
  • $\begingroup$ @supercat: It's entirely possible to combine the preprocessor into the same executable as the rest of the compiler. The standards phrase things in terms of "Phases of Translation", of which the first 6 are traditionally considered "preprocessor". $\endgroup$ Jan 30 '14 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.