January 7, 2011

cazador7907 cazador7907
Lab Rat
78 posts

[ANSWERED] Include Files

Page  
1

Ok.

I was working on a program this afternoon and noticed something odd. In the below example, GraphNode, Node and Edge all exist in the same folder in the project. However, for the GraphNode class to compile, I need to reference the full path to the Node and Edge classes.

  1. #include <Graph/node.h>
  2. #include <Graph/edge.h>
  3.  
  4. class GraphNode : public Node
  5. {
  6.      ......
  7. }

Is this by design?
 Signature 

Laurence -

 

17 replies

January 7, 2011

peppe peppe
Ant Farmer
1029 posts

Are you using “Edge” in that class definition?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

January 7, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

Whether or not you need to prepend the “Graph/” part to the include statement depends on the search path you set for your include files.

You could also try to @#include “edge.h”@. That will check for the file in the local directory.

January 7, 2011

DavidGOrtega DavidGOrtega
Lab Rat
25 posts

As Tobias stated check this link:

http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx

Use
#include “Graph/edge.h”

January 8, 2011

Panke Panke
Lab Rat
42 posts

The two versions of include (<> and “”) behave different. <> searches the include path and the behaviour of “” is compiler-dependent. But the directories searched by <> must be a subset of the directories searched by “”.

Is this by design?

So, yes, it is. My recommendation is to use <>, put the root directory of your sources into the include path and reference the files relative from this root.

January 8, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

Panke: The C/C++ standards do not specify the behavior of either #include <…> nor #include “…”. Actually the included stuff is not even defined to be a file! The compiler is free to replace “#include <stdio>” with some piece of text that was compiled into it if it feels like doing so… There is no requirement of <> being a subset of “” in any standard. So much for the theory:-)

In practice <> is a subset of “” in most compilers. In general <> searches paths build into the compiler or set up by the build system (gcc: -I/some/path or -isystem /some/path, other compilers may or may not use similar switches) plus some default system pathes (e.g. /usr/include in Linux). “” usually does search in the current source directory first before checking the system paths.

January 8, 2011

Volker Volker
Ant Farmer
5428 posts

According to K&R 2nd edition, appendix A.12.4 File Inclusion, “…” is a (not necessarily strict) superset of <..>:

Similarly, a control line of the form # include “filename” searches first in association with the original source file (a deliberately implementation- dependent phrase), and if that search fails, then as in the first form1. The effect of using ‘, \, or /* in the filename remains undefined, but > is permitted.

1 the first form is #include <filename>

[EDIT: fixed mistake, “…” is a superset, not a subset of <…>]

January 8, 2011

Taamalus Taamalus
Lab Rat
61 posts

This code guru, so, my two pennies
GCC compiler and it’s smaller brother MingW which comes with my Qt Creator has this to say about the preprocessor directive #include:


#include <file>
This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).
#include “file”
This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to the list of quote directories with the -iquote option.

Therefore, in my case, using Qt Creator with MingW, all none standard headers will be referenced with “…” . A compiler may not necessarily follow the paths as outlined in MSDN, unless I explicitly instruct my compiler to do so. (I am too lazy to do that)

 Signature 

... time waits for no one. - Henry

January 10, 2011

Wolf P. Wolf P.
Ant Farmer
354 posts

Good point, Taamalus.
C++ standards are sometimes (very) far from our real life.
The given environment determines the binding documentation, no matter how standards compliant it is.

This problem is reflected in the acronym RTFM [en.wikipedia.org] ;)

January 10, 2011

Andre Andre
Robot Herder
6417 posts

Wolf P. wrote:
Good point, Taamalus.
C++ standards are sometimes (very) far from our real life.
The given environment determines the binding documentation, no matter how standards compliant it is.

This problem is reflected in the acronym RTFM [en.wikipedia.org] ;)


So, I guess the correct answer to RTFM then, is to ask RWFM?

(the “W” standing for “Which”)

January 10, 2011

Wolf P. Wolf P.
Ant Farmer
354 posts

For Qt, /I/ havn’t read it nor searched for it ;)
But regarding my current main development environment, I have found that neither I nor my colleagues have read it early enough.

When working with Qt Creator, you should consult one of the documentations of /MinGW/ or /Visua Studio/, depending on your software installation.

January 10, 2011

Volker Volker
Ant Farmer
5428 posts

Andre wrote:
Wolf P. wrote:
This problem is reflected in the acronym RTFM [en.wikipedia.org] ;)

So, I guess the correct answer to RTFM then, is to ask RWFM?

(the “W” standing for “Which”)

That of the compiler(s) you use. As far as I know the popular ones tend to behave quite similar on include paths.

[EDIT: correct quotes]

January 10, 2011

Wolf P. Wolf P.
Ant Farmer
354 posts

There are two options, right?
If there are online versions, can anybody post the appropriate links to GCC and MSVC?

January 10, 2011

Panke Panke
Lab Rat
42 posts

GCC Handbook [gcc.gnu.org]

January 10, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

Wolf P.: You only use two compilers? You are lucky then!

Also note that the behavior of the compiler can change between versions (even though it should not change for fundamental things like include path handling:-).

January 10, 2011

Gerolf Gerolf
Robot Herder
3287 posts

MSDN for MSVS [msdn.microsoft.com]).aspx

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

Page  
1

  ‹‹ The difference between Qwidget object and pointer      [Moved/ANSWERED] STL::Map ››

You must log in to post a reply. Not a member yet? Register here!