Reputation: 2441
My c++ project in summary is composed of two directories and of course the main.cpp
:
Dataloader
containing dataloader.h and dataloader.cppAnalysis
containing analysis.h and analysis.cppin 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
Upvotes: 0
Views: 262
Reputation: 303
Well I've always been taught, and this makes sense, that you should put your includes in the file that depends on them.
So in your situation, I assume you use dataloader
to import data, and then the analysis
to analyze it. Well in this case, it makes more sense to include both of these in the main.
In comparison, I made an n by n tic tac toe game that includes a print_board()
function. This function relies on a header file with constants, colors.h
, to properly print the board. So in this circumstance, it makes more sense for my print_board.h
file to include the colors.h
file, because it is directly dependent on it, and nothing else is.
Upvotes: 1
Reputation: 206577
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.
Upvotes: 2