Blade
Blade

Reputation: 1110

Adding header file and related modification to the makefile

I have a c++ program and I'm trying to make a header file for it and take some functions out and put them on a separate file. I'm having some errors and below I have described my approach to it. I would appreciate if anyone can guid me on what might be wrong here.

I have a c++ program with following tree

.
├── main
|    └── mymain.cc
├── makefile
├── obj
├── solver
└── data

and following main file:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

extern "C" void solver_();

class DataHandler
{
 public:
    DataHandler() {}
    std::vector<std::vector<double> > Read2D(std::string fileName);
 };

int main( int argc, char **argv ) {

    DataHandler fileIO;
    std::vector<std::vector<double> > perm;
    perm = fileIO.Read2D("../../../data/perm.dat");


    return(0); 

}

    std::vector<std::vector<double> > DataHandler::Read2D(std::string fileName)
 {
    // Some codes . . .
 }

and following makefile:

# GNU Makefile
# Paths
MDIR=./main
SDIR=./solver
ODIR=./obj
_CASE=WorkCases/problem
CASE=$(SDIR)/$(_CASE)
TOP = .  

#
# set compilers
FC          =       ifort
CC          =       icc
CXX         =       icc
LINKLIB     =       -lstdc++ -std=c++11 -lifcore

FFLAGS          =       -fpp -O1 -DPTR_INTEGER8 -warn nousage -module $(ODIR)
CCFLAG          =       -O1 -DDATABASE_INTERFACE_LOWERCASE -DDATABASE_APPEND_UNDERSCORE
CXXFLAGS        =       -O1 

#
# Define rules to make
$(ODIR)/%.o : $(SDIR)/%.f90 
    $(FC) -c $(FFLAGS) $< -o $@

$(ODIR)/%.o : $(MDIR)/%.cc
    $(CXX) $(CXXFLAG) -c $< -o $@

#
# set executable name based on DB and CASE
EXEC    =   $(dir ${CASE})/$(basename $(notdir ${CASE})).out

#
# main files
MAIN_OBJ    =   $(ODIR)/mymain.o

#
# variables
_SOME_OBJ   =       sample1.o sample2.o sample3.o
SOME_OBJ    =   $(patsubst %,$(ODIR)/%,$(_SHARED_OBJ))

_OBJ     =  ${_SOME_OBJ} $(_CASE).PARAMS.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

MORE_OBJ = $(ODIR)/more.o

wlt_3d : ${SOME_OBJ} $(OBJ) $(MORE_OBJ) $(MAIN_OBJ)
    $(CXX) $(CXXFLAGS) $(OBJ) $(MORE_OBJ)  $(MAIN_OBJ) -o $(EXEC) $(LINKLIB)

I would like to add a header file and take the functions to another file. My modified tree:

.
├── main
|    └── mymain.cc
├── inc
|    └── DataHandler.hpp
├── src
|    └── DataHandler.cpp
├── makefile
├── obj
├── solver
└── data

my modified main:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

#include "DataHandler.hpp"

extern "C" void solver_();

int main( int argc, char **argv ) {

    DataHandler fileIO;
    std::vector<std::vector<double> > perm;
    perm = fileIO.Read2D("../../../data/perm.dat");


    return(0); 

}

my new header file:

#ifndef DH_HEADER
#define DH_HEADER

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

class DataHandler
{
 public:
    DataHandler() {}
    std::vector<std::vector<double> > Read2D(std::string fileName);
 };

#endif

and corresponding cpp file:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

#include "DataHandler.hpp"
    std::vector<std::vector<double> > DataHandler::Read2D(std::string fileName)
 {
    // Some codes . . .
 }

I'm modifying the makefile as below

# GNU Makefile
# Paths
MDIR=./main
SDIR=./solver
ODIR=./obj
SRC=./src
IDIR=./inc
_CASE=WorkCases/problem
CASE=$(SDIR)/$(_CASE)
TOP = .  

#
# set compilers
FC          =       ifort
CC          =       icc
CXX         =       icc
LINKLIB     =       -lstdc++ -std=c++11 -lifcore

FFLAGS          =       -fpp -O1 -DPTR_INTEGER8 -warn nousage -module $(ODIR)
CCFLAG          =       -O1 -DDATABASE_INTERFACE_LOWERCASE -DDATABASE_APPEND_UNDERSCORE
CXXFLAGS        =       -O1 

#
# c++ include files:
DEPS=$(IDIR)/DataHandler.hpp

#
# Define rules to make
$(ODIR)/%.o : $(SDIR)/%.f90 
    $(FC) -c $(FFLAGS) $< -o $@

$(ODIR)/%.o : $(MDIR)/%.cc $(DEPS)
    $(CXX) $(CXXFLAG) -c $^ -o $@

$(ODIR)/%.o : $(SRC)/%.cpp $(DEPS)
    $(CXX) $(CXXFLAG) -c $^ -o $@

#
# set executable name based on DB and CASE
EXEC    =   $(dir ${CASE})/$(basename $(notdir ${CASE})).out

#
# main files
MAIN_OBJ    =   $(ODIR)/mymain.o

_SRC_OBJ    =   DataHandler.o
SRC_OBJ     =   $(patsubst %,$(ODIR)/%,$(_SRC_OBJ))

#
# variables
_SOME_OBJ   =       sample1.o sample2.o sample3.o
SOME_OBJ    =   $(patsubst %,$(ODIR)/%,$(_SHARED_OBJ))

_OBJ     =  ${_SOME_OBJ} $(_CASE).PARAMS.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

MORE_OBJ = $(ODIR)/more.o

wlt_3d : ${SOME_OBJ} $(OBJ) $(MORE_OBJ) $(SRC_OBJ) $(MAIN_OBJ)
    $(CXX) $(CXXFLAGS) $(OBJ) $(MORE_OBJ)  $(SRC_OBJ) $(MAIN_OBJ) -o $(EXEC) $(LINKLIB) 

However, I get the following error.

icc  -c src/DataHandler.cpp inc/DataHandler.hpp -o obj/DataHandler.o
src/DataHandler.cpp(7): catastrophic error: cannot open source file 
"DataHandler.hpp"
  #include "DataHandler.hpp"
                        ^

compilation aborted for src/DataHandler.cpp (code 4)

Could anyone please give me a feedback on why this happens?

Upvotes: 2

Views: 224

Answers (1)

Olga Doronina
Olga Doronina

Reputation: 51

You forgot to add dependents folder to the compiler's include path. add -I$(dependents_folder_name) to compilation rule

Upvotes: 1

Related Questions