0

Confused about #include for seperate directories in C++ using Unix

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
csx
  • 1
  • 1
  • 1
  • 4
  • 2
    Set the include path properly and #include –  Nov 14 '14 at 18:33
  • _"I don't know how I can check that or find that out."_ Have a look at the makefile used for building. – πάντα ῥεῖ Nov 14 '14 at 18:34
  • Notice: if compiling with `gcc` or `g++`, try passing `-H` to the compiler, it will show the included files. – Basile Starynkevitch Nov 14 '14 at 19:06
  • possible duplicate of [What is the difference between #include and #include "filename"?](http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) – Vyktor Nov 15 '14 at 08:45

2 Answers2

1

Your question is partially answered here, let's assume you're using GCC (the same comparison for Visual Studio):

  • #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.

So when you want to include geometry.h using #include <> you just have to invoke gcc with additional -I argument, you just have to decide how "deep" do you want to go:

gcc -I                               #include
/abs/path/ws/B/inc/Normal            <geometry.h>
/abs/path/ws/B/inc                   <Normal/geometry.h>
/abs/path/ws/B                       <inc/Normal/geometry.h>
/abs/path/ws                         <B/inc/Normal/geometry.h>
/abs/path                            <ws/B/inc/Normal/geometry.h>

And if you want to use #include "" you may do that too:

Lets assume you're in /abs/path/ws/A/inc/your_header.h and want to include /abs/path/ws/B/inc/Normal/geometry.h:

#include                          path
"geometry.h"                      /abs/path/ws/A/inc/geometry.h
"../geometry.h"                   /abs/path/ws/A/geometry.h
"../../geometry.h"                /abs/path/ws/geometry.h
"../../B/geometry.h"              /abs/path/ws/B/geometry.h
"../../B/inc/geometry.h"          /abs/path/ws/B/inc/geometry.h
"../../B/inc/Normal/geometry.h"   /abs/path/ws/B/inc/Normal/geometry.h

If this doesn't work, you are probably in different folder, or have a typo somewhere.

I would personally go with the first solution (you may want to have your libraries "system wide" later and this way you just have to change one -I header in makefile without editing sources).

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
-1

as "PP/linear.h" is seen that means that we are in "ws/A/me/inc" so with .. we go to "me" with ../.. we go to "A" with ../../.. we go to "ws" then "../../../B/inc/Normal/geometry.h" should bring us to the desired position.

George Kourtis
  • 2,381
  • 3
  • 18
  • 28