23

Does it matter how I import header files? I've seen double quotes as well as arrows used.

#include <stdlib.h>
#include "Some_Header.h"

Does it matter if they're capitalized a certain way as well? Experimenting around with this, it seems neither matters, but I figure there must be a reason for tutorials doing it the way they do.

Another question is, (coming from Java here), how do I access a class outside the file it was defined in? Say, I have one.cpp and two.cpp.

In one.cpp:

class Something {
    ...

In two.cpp:

class SomethingElse {
    Something *example;
    ...

Like that? In Java you'd just preface a class name with "public." In C++, wrangling classes seems to be a bit more difficult..

derWilly
  • 455
  • 3
  • 15
arjs
  • 2,835
  • 4
  • 22
  • 18
  • 14
    @GMan: he never said that Java is C++, or C++ is Java. He just explained what he's used to in Java and wondered what the equivalent in C++ is. – Laurence Gonsalves Sep 18 '10 at 09:32
  • 11
    @GMan: It's natural for someone learning a new X to try to find equivalences with previous X's they've used. Yes, there arguably isn't a direct equivalence in this case, but there are many other things in Java that do have equivalences in C++. eg: if he asked "In Java I write `boolean x`, how do I do that in C++" would you give him a similar "Java isn't C++" answer, or would you just let him know that "boolean" is spelled "bool" in C++? I think your concern about people programming X in language Y is valid, but I think you're jumping the gun here. – Laurence Gonsalves Sep 18 '10 at 10:12
  • 3
    @GMan: Suggesting that he read a book isn't the issue. The issue is the attitude expressed by your "Seriously, Java isn't C++" comment. A large fraction of questions on SO are basic questions that can be answered by reading the right book or performing a Google search but that doesn't mean they should be met with rudeness. I suspect that the mention of Java hit a nerve with you for some reason. Get over it. – Laurence Gonsalves Sep 18 '10 at 19:58
  • 1
    You need to learn [how to organize code files in C++](http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798). – fredoverflow Sep 18 '10 at 09:15
  • @fredoverflow your link was super helpful and answered so many of my c++ building problems. Thank you! – NateW Oct 27 '17 at 06:25

5 Answers5

38

Angle brackets in #include directives means the search path is limited to the "system" include directories. Double quotes mean the search path includes the current directory, followed by the system include directories.

The case of the filename matters when your OS is using a filesystem that is case sensitive. It sounds like you might be using Windows or Mac OS X, where filenames are case insensitive by default.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
9

Angle brackets looks for the header in system header directories (e.g. /usr/include). Quotes is just an absolute or relative pathname, such as /path/to/header.h or ../headers/abc.h.

For accessing classes from other files, just #include the other file with the class. Be sure to structure your program so that no file is included more than once.

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • "Be sure to structure your program so that no file is included more than once." -> you can include them as often as you want as long as you use include guards, which you should. – fredoverflow Sep 18 '10 at 09:28
  • 2
    Note that class declarations should go in headers, not in .cpp. You should never #include a .cpp. – Matteo Italia Sep 18 '10 at 09:47
  • 1
    There is no technical difference between headers and source files; it is up to the developer how to use them. It's all an insignificant file name extension. – Delan Azabani Sep 18 '10 at 09:49
  • 2
    Well, after all, all the files are octet streams, let's just remove the extensions and all the metadata, who cares? It's up to the user decide how to open them... <_< I know that you could call a header even "A.exe" and the compiler wouldn't notice, but conventions are there to make life easier for everybody, breaking them for the sake of doing it's quite a stupid thing. – Matteo Italia Sep 18 '10 at 10:02
  • Not exactly. I'm saying that headers and source files can equally have the same stuff in them, no problem. Also, any good operating system can determine the file type without extensions ;) – Delan Azabani Sep 18 '10 at 10:05
  • 1
    You're missing the point; I'm saying that the extensions are there as indications on how to use the file, giving to a header a .cpp extension is at least confusing, both for programmers (who would be quite puzzled) and for IDEs (who would try to compile it on its own). – Matteo Italia Sep 18 '10 at 10:08
  • I must assume you weren't really implying that any operating system will burden itself with the work of parsing C++ source files to determine whether they represent headers or source (or equivalent for other languages with different extensions). That's why we just use extensions. – underscore_d May 07 '17 at 08:32
9

First the simple question:

Does it matter if they're capitalized a certain way as well?

In most cases, includes refer to files, and the compiler should be able to locate the file that you are including in the system. For that reason capitalization matters in all systems where the filesystem is case sensitive. If you want to keep a minimum of portability you should be consistent in the name of the files and the include. (All linux and mac os have case sensitive filesystems by default, in windows you can configure NTFS to be case sensitive also)

Now, does it actually matter how the file is named? No, it does not, as long as you are consistent in the inclusions. Also note that it is advisable to follow a pattern to ease the inclusion.

Does it matter how I import header files?

The standard is not really clear to this point, and different implementations followed different paths. The standard defines that they may be different, as the set of locations and order in which the compiler will search for the included file is implementation defined and can differ if the inclusion is with angle brackets or double quotes. If inclusion with quotes fails to locate the file, the compiler must fall back to process the include as if it had been written with angle brackets.

#include <x.h> // search in order in set1 of directories
#include "x.h" // search in order in set2 of directories
               // if search fails, search also in set1

That implies that if a file is only present in set1, both types of includes will locate it. If a file is present in set2 but not set1 only the quote include will locate it. If different files with the same name are present in set1 and set2, then each inclusion type will find and include a different file. If two files with the same name are present in the common subset of set1 and set2, but the ordering of the sets is different each inclusion type can locate a different file.

Back to the real world, most compilers will include only the current directory in set2, with set1 being all the system include locations (which can usually be extended with compiler arguments) In these cases, if a file is only present in the current directory, #include "a.h" will locate it, but #include <a.h> will not.

Now, whether that is the common behavior, there are some implied semantics that are idiomatic in C/C++. In general square brackets are used to include system headers and external headers, while double quotes are used to include local files. There is a grey zone on whether a library in the same project should be considered as local or external. That is, even if always including with double quotes will work, most people will use angle quotes to refer to headers that are not part of the current module.

Finally, while no compiler that I know of does it, the standard allows an implementation (compiler) not to produce the standard headers as real files, but process the inclusion of standard headers internally. This is the only case where theoretically #include "vector" could fail to include the definitions of the std::vector class (or any other standard header). But this is not a practical issue, and I don't think it will ever be.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
7

Question 1

Does it matter how I import header files? Does it matter if they're capitalized a certain way as well?

It doesn't matter, but usual practice is,

  • Use angle brackets for system headers.
  • User double quotes for User defined headers(Your own headers)

Questions 2 & 3

Another question is, (coming from Java here), how do I access a class outside the file it was defined in?

You need to place class definition in a header file and include that header file wherever you want to use the class. For your case, it would look like below.

//One.h
#ifndef ONE_H
#define ONE_H
class Something
{
public:
    void doSomething(){}

};
#endif

//Two.cpp
#include "One.h"
class SomethingElse
{
   SomeThing *example;
};
bjskishore123
  • 6,144
  • 9
  • 44
  • 66
  • 1
    `class Something { /* ... */ };` is actually a class *definition*, not a class *declaration*. A class declaration looks like `class Something;` (note the missing class body) and is often redundantly called a *forward declaration*. – fredoverflow Sep 18 '10 at 09:27
  • 4
    I think the case of the filenames would matter on a system like Linux, where they are case-sensitive. It won't matter on Windows, where file names are only case-preserving. – Merlyn Morgan-Graham Sep 18 '10 at 09:29
  • You quoted his second question as "question 1", answered his actual first question, and then moved on to his third question (calling it "question 2") – Laurence Gonsalves Sep 18 '10 at 09:30
  • @Fred: Agree with you. Corrected my answer accordingly. – bjskishore123 Sep 18 '10 at 09:31
  • @Laurence: Corrected saying Questions 2 & 3 :) – bjskishore123 Sep 18 '10 at 09:34
  • @Merlyn: Thank you. Good input from your side. – bjskishore123 Sep 18 '10 at 09:35
  • 2
    "It doesn't matter, but usual practice is" - actually, it *does* matter, AFAIK the compiler should search the headers included with brackets in the system/compiler headers path, and the ones included with quotes in the current file's path (or, at least, it tries it first). – Matteo Italia Sep 18 '10 at 09:44
  • 1
    You shouldn't prefix the include guard name with `_`; names like that are reserved. – Mike Seymour Sep 18 '10 at 11:24
0

Firstly, the #include is a C pre-processor directive and not strictly part of the C++ language as such. You can find out more about it here although this is specifically for the GNU C preprocessor so may be different from what you're using. I think you should always assume case-sensitivity in include files. Not doing so might make it difficult to port you code to a case sensitive OS such as UNIX.

The use of "" or <> is rather subtle as explained above and most time you will notice no difference. Using "" generally searches the current directory first. I tend not to use this as:

  • I know where my headers are - I always specify them with -I on the compile line.
  • I've been caught out before when a locally bodged copy of a header overrode the central copy I was hoping to pick up.

I've also noticed some side effects such as when using make to create dependency trees (I can't quite recall the issue - it did treat the different includes differently, following some and not others, but this was about 7 years ago)

Secondly, your question about how to reference functions in other files is answered here/

Community
  • 1
  • 1
Component 10
  • 10,247
  • 7
  • 47
  • 64