Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I detect unnecessary #include files in a large C++ project?

I am working on a large C++ project in Visual Studio 2008, and there are a lot of files with unnecessary #include directives. Sometimes the #includes are just artifacts and everything will compile fine with them removed, and in other cases classes could be forward declared and the #include could be moved to the .cpp file. Are there any good tools for detecting both of these cases?

like image 309
shambolic Avatar asked Sep 16 '08 16:09

shambolic


People also ask

How do I know which Programs are unnecessary?

Go to your Control Panel in Windows, click on Programs and then on Programs and Features. You'll see a list of everything that's installed on your machine. Go through that list, and ask yourself: do I *really* need this program? If the answer is no, hit the Uninstall/Change button and get rid of it.

How do I delete unnecessary?

An easy way to do this is to press Windows key + S to activate the search bar, type cleanup , and then click Disk Cleanup in the search results. Disk Cleanup is one of the fastest ways to delete unneeded files created by the Windows operating system.


2 Answers

While it won't reveal unneeded include files, Visual studio has a setting /showIncludes (right click on a .cpp file, Properties->C/C++->Advanced) that will output a tree of all included files at compile time. This can help in identifying files that shouldn't need to be included.

You can also take a look at the pimpl idiom to let you get away with fewer header file dependencies to make it easier to see the cruft that you can remove.

like image 66
Eclipse Avatar answered Oct 02 '22 08:10

Eclipse


PC Lint works quite well for this, and it finds all sorts of other goofy problems for you too. It has command line options that can be used to create External Tools in Visual Studio, but I've found that the Visual Lint addin is easier to work with. Even the free version of Visual Lint helps. But give PC-Lint a shot. Configuring it so it doesn't give you too many warnings takes a bit of time, but you'll be amazed at what it turns up.

like image 45
Joe Avatar answered Oct 02 '22 10:10

Joe