Search code examples
c++directoryinclude-path

Is #include <../header.h> a bad practice in C++?


My c++ project in summary is composed of two directories and of course the main.cpp:

  1. Dataloader containing dataloader.h and dataloader.cpp
  2. Analysis containing analysis.h and analysis.cpp

in dataloader.h I included analysis.h as follows:

#include<../Analysis/analysis.h>

My professor told me that it's a BIG NO NO!!!!!. We were trying to create a makefile and he was surprised. Why is it a bad practice? and how can I make the include simpler. I am working on code::blocks and gcc 4.8 compiler.

N.B. I noticed that code::blocks some how forgives include errors. When we tried to compiled it by using gcc command it went bad



Solution

  • I agree with your professor. Use of

    #include<../Analysis/analysis.h>
    

    makes the code brittle. If the code base is re-organized using a different directory structure, files containing such #include statements will fail to compile.

    Regarding:

    N.B. I noticed that code::blocks some how forgives include errors. When we tried to compiled it by using gcc command it went bad

    Add -I. to the compiler flags to resolve that problem.