tags:

views:

1131

answers:

6

I have just come across a Visual C++ option that allows you to force file(s) to be included - this came about when I was looking at some code that was missing a #include "StdAfx.h" on each .cpp file, but was actually doing so via this option.

The option can be found on the Advanced C/C++ Configuration Properties page and equates to the /FI compiler option.

This option could prove really useful but before I rush off and start using it I thought I'd ask if there are any gotchas?

+7  A: 

I would discourage from /FI (MSDN says it's called /FI . Not sure whether i looked at the right page though), simply because people or yourself reading the files don't notice a header is magically included anyway.

You can be sure this will cause much debugging time for someone that wants to figure out where specific macros come from, even though there are no #include lines at the top of the file.

Johannes Schaub - litb
Agreed. I would have said the exact same thing. :)
John Dibling
+1, also be explicit in the includes you need for your code and don't depend on users including other files before your .h's, nor in some included file indirectly including something you depend on.
David Rodríguez - dribeas
+1  A: 

I would say the opposite to litb above if you're using precompiled headers. If you use "stdafx.h" as your precompiled header and have code like this:

#include "afile.h"
#include "stdafx.h"

then you'll be spending an age trying to figure out why "afile.h" isn't being included. When using precompiled headers, all the includes and #defines are ignored up to the "stdafx.h". So, if you force include the "stdafx.h" then the above will never happen and you'll get a project that uses the precompiled option efficently.

As for litb's comment about finding macros, good IDE's usually have an option to jump to the definition of a symbol, wether it be a #define, function, class, etc.

Skizz

Skizz
+1  A: 

I'd side with litb: don't do the forced includes. Having the code be explicit makes it easier to know what's going on for a new user. It also makes it easier to know what's going on if you ever need to port the code to a new platform.

Note that if the code uses templates, Visual Studio usually can't track down the definitions correctly. Perhaps 2010 will be better, but even VS 2008 is problematic on this front.

Mr Fooz
+1  A: 

Save this function for when something weird comes up - like if you want to include a header in a generated source file. Even then there are likely better ways.

Eclipse
A: 

I wouldn't use it that often but it has its uses. I have used it to add a header that suppressed some warnings to all cpp files so that I could turn on /W4 or /Wall for the project and not have to edit all of the cpp files to include the warning suppression header first. Once eveything was working I DID go back and edit all the cpp files but for a proof of concept /FI was useful.

Likewise you can use it to force a precompiled header into cpp files in some build configurations but not in all (in case you want to have a build configuration that DOESNT use precompiled headers and that makes sure that each cpp only includes exactly what it needs to). However using #pragma hdrstop is, IMHO, a better way to achieve this.

I've talked about all of this on my blog here: http://www.lenholgate.com/archives/000345.html in a little more detail.

Len Holgate
+1  A: 

Force includes is also helpful for automatically generated source files. Our system uses a tool that generates many source files but they don't include our pre-compiled header file. With "force includes" compilation of these files is now faster.

Optionally, it would be possible to write a script to insert the #include line in those files after generation and before compilation. But why go to that trouble?