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

时间:2015-12-10 06:13:49

标签: c++ c-preprocessor

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?

1 个答案:

答案 0 :(得分:8)

#include语句有三种形式。

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

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

# include pp-tokens new-line

其中pp-tokens必须扩展为前两种形式之一。

您可以使用:

#include IOSTREAM

并使用-DIOSTREAM="<iostream>"-DIOSTREAM="<iostream.h>"进行编译,具体取决于您使用的编译器版本。

但是,你不能使用

#include <iostream.h>

并使用-Diostream.h=iostream进行编译。

这有几个问题。

  1. iostream.h不是有效的预处理器宏。
  2. #include语句的形式不适合宏扩展。
  3. 如果您已准备好迁移代码库以使用新的C ++标头,那么最好使用您喜欢的脚本方法将所有旧式C ++标头更改为新的C ++标头。