Visual C ++ option 'Force Includes'

I just came across a Visual C ++ option that allows you to force the inclusion of files (files) - this happened when I looked at some code that #include "StdAfx.h" in every .cpp file, but was actually doing it through this option.

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

This option can be really useful, but before I leave and start using it, I thought I would ask if there are any errors?

+7
c ++ visual-c ++
source share
6 answers

I would refuse / FI ( MSDN says it's called / FI. I'm not sure if I looked at the correct page) simply because people or the people reading the files themselves do not notice that the header is magically included anyway.

You can be sure that this will cause a lot of debugging time for those who want to find out where specific macros came from, even if there are no #include lines at the top of the file.

+12
source share

I would say the opposite to litb is higher if you use precompiled headers. If you use "stdafx.h" as your precompiled header and have this code:

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

then you will spend age trying to understand why "afile.h" does not turn on. When using precompiled headers, all included and #defines are ignored until "stdafx.h". Thus, if you force "stdafx.h" to work, the above will never happen, and you will get a project that effectively uses the precompiled option.

As with the vivid commentary on searching for macros, a good IDE usually has the ability to go on to define a character, be it #define, function, class, etc.

+15
source share

I would be backlit: do not force the inclusion. Having explicit code makes it easier to find out what is happening for a new user. It also makes it easier to understand what happens if you ever need to port your code to a new platform.

Note that if the code uses templates, Visual Studio usually cannot properly track the definitions. 2010 may be better, but even VS 2008 is problematic on this front.

+2
source share

Save this function when something strange appears - for example, if you want to include a header in the generated source file. Even then there are probably better ways.

+1
source share

I would not use it so often, but he used it. I used it to add a header that suppressed some warnings for all cpp files so that I could include / W 4 or / Wall for the project and didn't have to edit all cpp files to include the warning header first. When eveything worked, I went back and edited all the cpp files, but it was useful to prove the / FI concept.

Similarly, you can use it to force pre-compilation of headers into cpp files in some build configurations, but not all (in case you want to have a build configuration in which DOESNT uses pre-compiled headers, and this ensures that every cpp includes just what is needed). However, using #pragma hdrstop is IMHO, the best way to achieve this.

I talked about all this on my blog here: http://www.lenholgate.com/blog/2004/07/fi-stlport-precompiled-headers-warning-level-4-and-pragma-hdrstop.html in more detail.

+1
source share

Force includes is also useful for automatically generated source files. Our system uses a tool that generates many source files, but does not include our precompiled header file. With force includes, compiling these files is now faster.

Optionally, one could write a script to insert the #include line into these files after generation and before compilation. But why go to this trouble?

+1
source share

All Articles