How to get the preprocessed C / C++ source files?

12 06 2008

Everybody knows that before the real compilation, the source file will undergo the stage called preprocessing. All the compiler preprocessors such as #include, #define etc will be processed in this stage and that intermediate preprocessed file, which is the real source – will be fed to compiler for compiling. Its a bit difficult to troubleshoot problems with preprocessor( especially macro expansions ) because, the processed intermediate file is not available. But is to possible to get it?

Ofcourse. You can use the compiler option /P. Take the project settings, In the C/C++ tab add the /P to compiler options. See the screenshot below.

Now if you take your source folder, you can see files with extension *.i
So if you’ve some trouble in macro expansion, just generate the intermediate file and check.

Open the generated intermediate file and see the contents. I’m sure you can’t believe your eyes. πŸ˜‰

Targeted Audience – Beginners.



5 responses

16 06 2008
Nishad S

Really nice and useful tip.

But got link error when I used that. I created a dlg based app. And given the /P option and built the source. Then I got a link error LINK : fatal error LNK1104: cannot open file “.\Debug\PreProcExp.obj”

If I remove the /P option then I will get the build successfully.

Anyway I could see the intermediate macro expanded files… πŸ™‚

17 06 2008

Nishad, You are right. Me too had the same link error. But the intermediate files are generated. Happy to know that the /P serve your need. πŸ˜‰

25 06 2008

During this process actual object file generation is not done. Thats why the link error. This /P flag can be used evenif some compilation errors are present in the source file πŸ™‚

So this flag is only checking the preprocessed output of an input file.

25 06 2008

So this flag is used only for checking the preprocessed output of an input file.

19 06 2009
Professor Rodolfo Martinez

It would be highly desirable for the IDE to generate the preprocessed output in a specified (fixed name) file and then continue with the compiling and linking process without resulting later in link errors as reported here. By default, the file would contain the preprocessed output of the last file compiled. With this approach, resources available for debugging a program would include this valuable preprocessed information without the need to set up switches or other complications such as linking errors downstream.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: