Sunday, October 10, 2010

VC++ Tip: Show Includes

The following is a useful Visual C++ tip that solves a frequent problem any VC++ developer might face.
There are time in which you get errors from files you have never created, or included into your codes.
This can be very frustrating and hard to debug especially if that file is burried deep into #include tree.

Consider the following example where you get an error such as
c:\program files\microsoft visual studio 8\vc\include\math.h(486): error C2084: function 'long abs(long)' already has a body

You might be scratching your head to figure out what does this mean knowing that you have never included "math.h" in your code.
One way of proceeding is to go open each nested #include file to determine where this file is include. However you will soon discover
that you have entered a complex labyrinth in which the way out is so difficult to find.

Another easier approach is tell the compiler to show the list of all included files used in the comilation:
1.Open the project's Property Pages dialog box.
2.Click the C/C++ folder.
3.Click the Advanced property page.
4.Modify the Show Includes property to 'yes'






On compilation you get a list like the following:
:c:\globalProjects\localProjects\srcCtrl\srv\src\myproj\util\net\ServerBase.h
: c:\globalProjects\localProjects\srcCtrl\srv\src\myprojTools\myprojUtils.h
: c:\globalProjects\localProjects\srcCtrl\srv\src\myproj\util\net\NetworkConnection.h
: :c:\globalProjects\localProjects\srcCtrl\srv\src\myproj\util\Obj.h
: : C:\Dev\Dependencies\thrdParty\extension\thrdParty\stlport\stl\_threads.h
: : :C:\Dev\Dependencies\thrdParty\extension\thrdParty\stlport\stl\_cstddef.h
: : : C:\Program Files\Microsoft Visual Studio 8\VC\include\stddef.h
: : : :C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
: : : : C:\Program Files\Microsoft Visual Studio 8\VC\include\sal.h
: : : : C:\Program Files\Microsoft Visual Studio 8\VC\include\crtassem.h
: : : : C:\Program Files\Microsoft Visual Studio 8\VC\include\vadefs.h
: : :C:\globalProjects\thrdParty\extension\stlport\stl/_cstdlib.h
: : : C:\Program Files\Microsoft Visual Studio 8\VC\include\stdlib.h
: : : :C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
: : : :C:\Program Files\Microsoft Visual Studio 8\VC\include\limits.h
: : : : C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
: : : C:\globalProjects\thrdParty\extension\stlport\stl\_cmath.h
: : : :C:\Program Files\Microsoft Visual Studio 8\VC\include\math.h
: : : : C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
>c:\program files\microsoft visual studio 8\vc\include\math.h(486) : error C2084: function 'long abs(long)' already has a body


In here you will find the whole tree of #include before the error. Notice how deep math.h is include far below ServerBase.h.


It is extremely difficult to detect its presence without the use of this option.

No comments:

Post a Comment