37

I am working on a project and I keep getting stumped on how I am supposed to import files from a different directory. Here is how some of my files are organized:

-stdafx.h
-core/
-->renderer.cpp
-shapes/
-->sphere.h
-->sphere.cpp

how can i access the stdafx.h and shapes/sphere.h from the core/renderer.cpp?

Stas Jaro
  • 4,747
  • 5
  • 31
  • 53
  • Be wary of following suggestions to use the `../` notations. See [What are the benefits of a relative path for a header](http://stackoverflow.com/questions/597318/). – Jonathan Leffler Dec 24 '11 at 05:58

4 Answers4

37

There are many ways. You can #include "../stdafx.h", for instance. More common is to add the root of your project to the include path and use #include "shapes/sphere.h". Or have a separate directory with headers in include path.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • It's somewhere in settings. As Dave points out, it's s `/I` option, but what you're looking for is probably something in project settings or whatever it's called, haven't seen Visual Studio for a while. – Michael Krelin - hacker Dec 23 '11 at 23:33
22

One (bad) way to do this is to include a relative path to the header file you want to include as part of the #include line. For example:

#include "headers/myHeader.h"
#include "../moreHeaders/myOtherHeader.h"

The downside of this approach is that it requires you to reflect your directory structure in your code. If you ever update your directory structure, your code won’t work any more.

A better method is to tell your compiler or IDE that you have a bunch of header files in some other location, so that it will look there when it can’t find them in the current directory. This can generally be done by setting an “include path” or “search directory” in your IDE project settings.

For Visual Studio, you can right click on your project in the Solution Explorer, and choose “Properties”, then the “VC++ Directories” tab. From here, you will see a line called “Include Directories”. Add your include directories there.

For Code::Blocks, go to the Project menu and select “Build Options”, then the “Search directories” tab. Add your include directories there.

For g++, you can use the -I option to specify an alternate include directory.

g++ -o main -I /source/includes main.cpp

The nice thing about this approach is that if you ever change your directory structure, you only have to change a single compiler or IDE setting instead of every code file.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Pushpak Sharma
  • 609
  • 6
  • 10
13

You can either use relative paths:

#include "../stdafx.h"
#include "../shapes/sphere.h"

or add your project directory to your compiler include path and reference them like normal:

#include "stdafx.h"
#include "shapes/sphere.h"

You can use the /I command line option to add the path or set the path in your project settings.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
0

You can use g++ -I /source_path /path_of_cpp to compile. /source_path is the path of the header file. Additionally, you can include the directory in which the header file is located in CPATH.

You can locate the header file by entering the following in the terminal locate header.h. The folder thus obtained can be extorted to CPATH using export CPATH = /source_directory. Replace /source_directory with the path of the directory obtained from locate header.h