Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a file from another folder?

Tags:

In my current project I have separated my class files and my header files. My project structure currently looks like this:

  • Project

    • Source
      • src
        • class1.cpp
        • class2.cpp
      • main.cpp
    • Header
      • include
        • class1.h
        • class2.h

My problem is that I do not know how to include the header files into the class files. Am I unable to link to headers that are not at the same level or in a child folder? Or is there some way to go from the project root and work my way down? For instance:
#include "Project/Headers/include/class1.h" inside the class1.cpp file

like image 993
Sheldon Allen Avatar asked Jul 30 '11 18:07

Sheldon Allen


People also ask

How do I put files in another folder?

You can find this option under Project Properties->Configuration Properties->C/C++->General->Additional Include Directories. and having it find it even in lib\headers. You can give the absolute or relative path to the header file in the #include statement.

How do I include a file in a different folder in PHP?

The way I do it is visual. I put my mouse pointer on the index. php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.

How do I include a file in a different directory in C++?

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.

How do you access a Python file from another folder?

The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module .


1 Answers

Assuming you want class1.cpp to include class1.h you would do something like this

#include "../../Header/class1.h" 

The .. tells the tells the OS to jump 1 directory up when the compiler asks for the file.

like image 172
Pepe Avatar answered Oct 07 '22 20:10

Pepe