41

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?

1

4 Answers 4

41

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
25

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.

Comments

16

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.

1 Comment

how do i add the project directory? Sorry I'm new to C++
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.