Posts tagged preprocessor

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

5


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.

Visual C++ Preprocessor null directive

0

Icon Description
Well, add an empty # symbol to your #include list and compile the project. For e.g. see the following #include list.

#include “stdafx.h”
#include “Dialog.h”

#
#include “DialogDlg.h”

It will compile without showing any errors. Weird?

icon_underthehood.jpg
The single “#” in a line is called null directive. It has no effect. Indeed, its not a bug. ;)
But i can’t find the answer for question… What is the purpose?

Log path informations to eventlog, Even better.

1

Icon Description
In most of our projects, when some error occurs, it’s being logged to the evenlog with line number and filename. Its done by using __FILE__ and __LINE__ macro.

But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder – “C:\Jijo\Build\MyProduct”, the __FILE__ macro will include this full path and finally in eventlog will contain funny paths which the end-user might see. You can see some 3ed party eventlogs in event viewer which contains authors name in path. :)

Icon How Can I Do It?
You can use #line directive to modify __LINE__ and __FILENAME__. The syntax is as follows,
#line lineno “FileName”

Please see an example below.

#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"

Icon Note
While experimenting I’ve found that – Its safe to use #line after all #includes. If its used at the top of file, It can be reinitialized by the include files. At first it was not working for me. Latter works. Anyway take care.

Go to Top