Tuesday, May 13, 2008

Wanna see how your MACROS are expanded?

#define

#include

#if

#ifdef

These, you know, are some of the preprocessor directives that we normally get to see in any program. There times when you feel its necessary to see what exactly the preprocessor has understood from the directive. For example, you might wanna know if your MARCO got defined as expected. Consider the code below.

 

#define MACRO1 1 + 2

#define MACRO2 1 - MACRO1

int main(){

int a=MACRO2;

cout << a << endl;

return 0;

}

To see the preprocessor output, use the gcc with -E option to compile the code. The output will be printed on screen itself.

[balaji1@cheud216][/home/balaji1]$ gcc -E temp.C

# 1 "temp.C"

# 1 "<built-in>"

# 1 "<command line>"

# 1 "temp.C"

 

int main(){

int a=1 - 1 + 2;

cout << a << endl;

return 0;

}

Likewise, if you have a #include <iostream.h>, you'll get to see the whole of iostream.h printed along with your code after 'gccing' as mentioned above.

 

The -E option can be used with any other compile time option in gcc (or g++). In case a make command is used for compilation, you may get to see gcc or g++ being used with many other options to compile a file. Just include -E to all those options and do a compilation again to get the preprocessed output.

0 Comments:

Post a Comment

<< Home