Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to #include headers?

I looked up header file examples, but could only find simple ones with nothing to include.

So my question is where do I #include stuff like string and vector? In the .h or the .cpp file? Or maybe in both?

like image 326
jabk Avatar asked Sep 29 '14 08:09

jabk


People also ask

Is it correct to say where to?

English, U.S. Strictly speaking, "where" is an adverb. Adding prepositions to it ("to", "at") would be treating it like a noun or pronoun (and some people do this in speech). In standard English, the preposition is superfluous and "wrong".

What is the meaning of whereto?

Definition of whereto (Entry 1 of 2) : to what place, purpose, or end whereto tends all this— William Shakespeare. whereto. conjunction.

What does mean where you at?

Where-you-at definition. Filters. (proscribed, nonstandard, slang, set phrase) "Where are you?" Used as an inquiry of location, especially figuratively.

What is correct to or too?

To is a preposition with several meanings, including “toward” and “until.” Too is an adverb that can mean “excessively” or “also.” Just to be clear: two is pronounced the same as to and too, but it can't be used instead of either of them because it's a number.


2 Answers

Anywhere you need.

If you need something declared in a header file, include them in the .h file. Otherwise just in the .cpp file.

Note that including a .h file is just a textual replacement and the contents of the included .h file will be entirely inserted at the beginning of the file where they've been included. at the precise point of the #include line. It is good practice to include a project-belonging headers before the standard library ones and never include a header if you don't need it.

Last thing you should keep in mind is that when working with large projects including many headers in a .h file shared by many translation units can increase compilation times if the header gets modified. It's usually preferred to just include what you strictly require in the appropriate files (either .cpp or .h). Precompiled headers might also help but it's off-topic to your question.

Finally: don't rely on "this header has been included somewhere else and I'm already including it through a second header" because it could render dependencies-tracking hard and favor circular dependencies when the project grows.

like image 115
Marco A. Avatar answered Oct 04 '22 20:10

Marco A.


where do I #include stuff like string and vector? In the .h or the .cpp file?

There are multiple issues to consider in this, especially when projects get bigger (i.e. the bigger your project is, the more this affects you).

Personally, I follow these rules:

  • if code needs a header to compile, then you need to include it (if header declares things with std::string in the API, you will have to include string and the same goes for the C++ file)

  • do not include headers that are not needed (i.e. not "both" - if you include a header in your .h file, then include your .h file, you should be fine).

  • organize headers prioritizing for your project's files. this means if you have in a C++ file local project headers, and std and boost headers, you should (probably) include local project headers first, then boost, then std.

    This is because the std headers will be the most tested/stable ones, and the most used API (this is a blind supposition on my part). If you were to include std headers first, and then project headers (for example), since the replacement is textual, you could get away with not adding the include in the project header. This would basically mask an error, because you would end up having to include the std headers before the local project header in all other cpp files from now on.

like image 29
utnapistim Avatar answered Oct 04 '22 20:10

utnapistim