{"id":18,"date":"2014-03-07T12:58:00","date_gmt":"2014-03-07T12:58:00","guid":{"rendered":"http:\/\/peintinger.com\/?p=18"},"modified":"2015-06-13T18:22:58","modified_gmt":"2015-06-13T18:22:58","slug":"organizing-cc-includes","status":"publish","type":"post","link":"https:\/\/peintinger.com\/?p=18","title":{"rendered":"Organizing C\/C++ includes"},"content":{"rendered":"<p>After starting my new job programming in a big software project I spent some thought on organizing includes and give a recommendation. Here&#8217;s what I&#8217;ve come up with. As always, some things are obvious, some are not&#8230;<\/p>\n<ol>\n<li>You should only include what is necessary to compile the source code. Adding unnecessary<br \/>\n includes means a longer compilation time, especially in large projects.<\/li>\n<li>Each header and corresponding source file should compile cleanly on its own. That<br \/>\nmeans, if you have a source file that includes only the corresponding header, it<br \/>\nshould compile without errors. The header file should include not more<br \/>\nthan what is necessary for that.<\/li>\n<li>Try to use forward declarations as much as possible. If you&#8217;re using a<br \/>\nclass, but the header file only deals with pointers\/references to<br \/>\nobjects of that class, then there&#8217;s no need to include the definition of<br \/>\n the class. That is what forward declarations are designed for!<\/p>\n<p>\/\/ Forward declaration<\/p>\n<p>class MyClass;<\/p>\n<p><\/p>\n<\/li>\n<li>Note that some system headers might include others. But apart from a few<br \/>\n exceptions, there is no requirement. So if you need both<br \/>\n&lt;iostream&gt; and &lt;string&gt; include both, even if you can<br \/>\ncompile only with one of them.<\/li>\n<li>To prevent multiple-inclusions, with loops and all such attendant horrors is having an #ifndef guard.\n<p>#ifndef _FOO_H<br \/>\n#define _FOO_H<br \/>\n&nbsp; &#8230;contents of header file&#8230;<br \/>\n#endif<\/p>\n<\/li>\n<li>The order in which the includes appear (system includes and user includes) is up to the coding standard you follow.<\/li>\n<li>If you have the choice of picking a coding standard regarding the order at the beginning of a new project, I recommend to go from local to global, each<br \/>\nsubsection in alphabetical order. That way you can avoid introducing<br \/>\nhidden dependencies. If you reverse the order and i.e. myclass.cpp<br \/>\nincludes &lt;string&gt; then &lt;myclass.h&gt;, there is no way to catch<br \/>\nat build time that myclass.h may itself depend on string. So if later someone includes myclass.h but does not need string, he&#8217;ll<br \/>\nget an error that needs to be fixed either in the cpp or in the header<br \/>\nitself.&nbsp;<\/li>\n<li>So the recommended order would be:\n<ul>\n<li>header file corresponding to its .cpp file<\/li>\n<li>headers from the same component<\/li>\n<li>headers from other components<\/li>\n<li>system headers<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\nIf you use the <a href=\"http:\/\/www.eclipse.org\/\" target=\"_blank\">Eclipse IDE<\/a> (which I highly recommend), you can use a very nice feature that helps you organizing includes (&#8220;Source -&gt; Organize Includes&#8221;).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After starting my new job programming in a big software project I spent some thought on organizing includes and give a recommendation. Here&#8217;s what I&#8217;ve come up with. As always, some things are obvious, some are not&#8230; You should only include what is necessary to compile the source code. Adding unnecessary includes means a longer &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/peintinger.com\/?p=18\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Organizing C\/C++ includes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[14,11,15],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-development","tag-include"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6W04n-i","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peintinger.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":1,"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/peintinger.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/peintinger.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peintinger.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peintinger.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}