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.

Automatically link required libraries.

11 04 2008

In our projects we use number of 3ed party API’s especially from platform SDK and other components and one burden is finding the libraries for those API’s. For Microsoft provided API’s since they have strong documentation, finding the library for particular API is not so tedious. But for other vendors, It may be. We need to dig a lot for finding the lib of that particular API. How can we get rid from this?

You can you #pragma comment( lib, “LibraryName” ) to specify the necessary library file in the header file itself. For e.g. See the following header file.

// EventLog.h
class EventLog
#ifdef _DEBUG
    // If it’s a debug version…
    #pragma comment( lib, "MyEventLogLibd.lib" )
    // Ooooh!!! Its release version.
    #pragma comment( lib, "MyEventLogLib.lib" )

If we include the EventLog.h header file, the library required will be automatically included. I dream for the day on which all SDK headers auto includes their libraries… 😉

Target audiance – Intermediate.

Integers with fixed size.

1 04 2008

Icon Description
While manipulating integer, in some cases we are worried about the size of int. Because, it depends the word length of processor. For instance while generating a binary message for your hardware etc.

Of course you have some macros such as BYTE etc which ensures the size in all processors word lengths, but will you get the real feel of a built-in integer?

Icon How Can I Do It?
Microsoft had provided __intn data types for integer values with different lengths. They are,

// Directly from MSDN.
__int8 nSmall;      // Declares 8-bit integer
__int16 nMedium;    // Declares 16-bit integer
__int32 nLarge;     // Declares 32-bit integer
__int64 nHuge;      // Declares 64-bit integer

You can use them without worrying about the length.

Targeted Audience – Beginners.