Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse C++ including header file from my source folder

I'm pretty new to C++ and Eclipse in general so I apologise if I'm missing something fairly obvious.

The problem I'm having is that I'm trying to include a header file in one of my source files but they're in different folders in my project directory. I have no idea how I should be including them. I've uploaded an image showing my problem with the header file I want to include highlighted.

enter image description here

If someone could tell me what '#include' statement I should be using them that would be brilliant.

Thanks!

like image 899
Joseph Little Avatar asked Nov 08 '12 21:11

Joseph Little


People also ask

HOW include header file in C Eclipse?

Select C/C++ General -> Path and Symbols. Select Includes tab. In Languages list, select 'GNU C' or whatever C compiler tool chain you use. Press 'Add...' button and add the directory for the include files.

How do you include a header file in AC source file?

You request to use a header file in your program by including it with the C preprocessing directive #include, like you have seen inclusion of stdio. h header file, which comes along with your compiler.

Where are header files stored in C?

These are the directories that gcc looks in by default for the specified header files ( given that the header files are included in chevrons <>); 1. /usr/local/include/ --used for 3rd party header files. 2. /usr/include/ -- used for system header files.

Can we create external C header files and include them?

So the question arises, is it possible to create your own header file? The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs.


2 Answers

When you create subfolders in your src folder then each cpp file is compiled in that folder it is located in. Thus, any "" includes need to specify the relative path to get from that folder to another.

In your case, to get from inside the FileInOut folder you need to go back one level and then into the Statistics folder

eg

#include "../Statistics/Statistics.h"

Another alternative is, if you are keeping your includes in your src directory, to add the src directory to the include path. Now when you include you need only specify the path from the src root.

eg.

#include "Statistics/Statistics.h"
like image 44
Dunes Avatar answered Oct 04 '22 01:10

Dunes


There are a couple of different options to make this work. Simplest is to change the #include to

#include "../Statistics/Statistics.h"

This will work without any other modifications. However, if you move either file, or somehow change the relative path between the two, this will break.

Alternately, you can add the path to the Statistics folder to your compiler's include file search path. Right click on the project name, select Properties -> C/C++ Build -> Settings and then find the includes files path option for your compiler. For g++, it is -I<path/to/include/folder>. Adding this will make the #include statement work as you currently have it.

A very similar option to the second one is to add the path to the src folder (instead of the Statistics folder) to the includes search path. In this case, you'll have to change the statement to

#include "Statistics/Statistics.h"
like image 169
Praetorian Avatar answered Oct 04 '22 03:10

Praetorian