sackoverfowl
sackoverfowl

Reputation: 71

Is it possible to alter #include filenames using #define?

I'm working with some old C++ code that, apparently, pre-dates the standardization and move from iostream.h to iostream, and similarly for other includes. Consequently, my relatively modern version of g++ fails when trying to #include <iostream.h>, etc.

I'm curious if it's possible to use the preprocessor to change instances of iostream.h to just iostream, via the command line. I've tried appending -Diostream.h=iostream to g++, but that doesn't seem to alter the include statements.

I'm guessing it's not possible for the preprocessor to modify include statements?

Upvotes: 7

Views: 145

Answers (1)

R Sahu
R Sahu

Reputation: 206567

There are three forms of the #include statement.

# include "h-char-sequence" new-line

# include <h-char-sequence> new-line

# include pp-tokens new-line

where pp-tokens must expand to one of the first two forms.

You could use:

#include IOSTREAM

and compile with -DIOSTREAM="<iostream>" or -DIOSTREAM="<iostream.h>" depending on the version of compiler you are working with.

However, you can't use

#include <iostream.h>

and compile with -Diostream.h=iostream.

There are couple of problems with that.

  1. iostream.h is not a valid preprocessor macro.
  2. The form of the #include statement is not appropriate for macro expansion.

If you are ready to migrate your code base to use the new C++ headers, you will be better off using your favorite scripting method to change all the old style C++ headers to new C++ headers.

Upvotes: 8

Related Questions