Questions tagged [include]

21 questions
57
votes
8 answers

Is it good practice to rely on headers being included transitively?

I'm cleaning up the includes in a C++ project I'm working on, and I keep wondering whether or not I should explicitly include all headers used directly in a particular file, or whether I should only include the bare minimum. Here's an example,…
futlib
  • 2,107
  • 3
  • 21
  • 26
53
votes
7 answers

How can I prevent header hell?

We are starting a new project, from scratch. About eight developers, a dozen or so subsystems, each with four or five source files. What can we do to prevent “header hell”, AKA “spaghetti headers”? One header per source file? Plus one per…
22
votes
7 answers

Why do we need to include the .h while everything works when including only the .cpp file?

Why do we need to include both the .h and .cpp files while we can make it work solely by including the .cpp file? For example: creating a file.h containing declarations, then creating a file.cpp containing definitions and including both in…
reaffer
  • 355
  • 1
  • 2
  • 5
11
votes
7 answers

Ensuring that headers are explicitly included in CPP file

I think it's generally good practice to #include the header for any types used in a CPP file, regardless of what is already included via the HPP file. So I might #include in both my HPP and CPP, for example, even though I could still…
sourcenouveau
  • 6,460
  • 4
  • 29
  • 44
7
votes
1 answer

When defining directory path, should a trailing slash be included?

Say I'm defining a directory and then including files from it. Is it better practice to do: define('PATH', 'C:/xampp/htdocs/includes/'); require PATH.'header.php; or: define('PATH', 'C:/xampp/htdocs/includes'); require PATH.'/header.php;
user107146
6
votes
2 answers

Is including headers inside a namespace always a bad idea?

The replies in this post come very strongly against including headers inside a namespace and Doxygen is confused if that is done (which suggests that its team did not consider that usage either). I would like to ask whether including headers inside…
AlwaysLearning
  • 543
  • 4
  • 8
5
votes
3 answers

Detecting header inclusion chains and dependencies in C++

File Top.h #include ...some code... File Bottom.h: #include "Top.h" void someFunction() { string s = new String(); ... } The Bottom.h does not contain an include for the string header, but the code will work because it's implicitly…
Karlovsky120
  • 307
  • 2
  • 9
5
votes
5 answers

Is it bad to place "include directive" within main function?

It is always said that the include directives should be placed at the beginning of a script. The main reason is to make the functions available throughout the script. Regardless of this fact, is it bad to place an include directive within the main…
Googlebot
  • 3,173
  • 5
  • 19
  • 14
4
votes
1 answer

The case against path expressions in #include directives

I am preparing for a discussion with my fellow programmers which will be about their use of the C/C++ #include directive. The codebase which I have to retrofit to Automotive standards is using includes of the form #include .…
Vroomfondel
  • 367
  • 2
  • 10
4
votes
1 answer

Should I specify my header include path in the source code, or as a project option?

Basically, I am asking, whether my code should say #include “../libs/src/my_lib.h” or #include “my_lib.h” with a complier option of -I ../libs/src/ I feel (reasonably strongly) that the former is preferable, because: it is independent…
4
votes
1 answer

Including local headers first

So I read up on the ordering of your includes, and this guy suggested you include your local header first so as to make sure it doesn't have prerequisites. Ok, I get that. I'm on board. The whole compartmentalization thing is good. But I've got this…
Philip
  • 6,742
  • 27
  • 43
3
votes
2 answers

Include own header file first or last? Any technical reasons?

I was revisiting my question How can I prevent header hell?, when I noticed a comment which said A good technique for ensuring a header is independent is having a rule that the source file always includes its own header first. This will catch cases…
3
votes
1 answer

Single complex or multiple simple autoload functions

Using the spl_autoload_register(), should I use a single autoload function that contains all the logic to determine where the include files are or should I break each include grouping into it's own function with it's own logic to include the files…
3
votes
1 answer

Handling bugs, quirks, or annoyances in vendor-supplied headers

If the header file supplied by a vendor of something with whom one's code must interact is deficient in some way, in what cases is it better to: Work around the header's deficiencies in the main code Copy the header file to the local project and…
supercat
  • 8,335
  • 22
  • 28
2
votes
3 answers

Should I ensure my libfoo library's include file can be included as foo/foo.h?

I'm maintaining a small C library; let's call it libfoo. It has quite a few users (actually it's complicated - it's the main fork of a highly popular library which has been abandoned); and it sports a spiffy CMake-based build mechanism. My repo…
einpoklum
  • 2,478
  • 1
  • 13
  • 30
1
2