Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
sackoverfowl Avatar asked Dec 10 '15 06:12

sackoverfowl


1 Answers

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.

like image 138
R Sahu Avatar answered Oct 19 '22 23:10

R Sahu