我有一个c ++程序,我试图为它创建一个头文件并取出一些函数并将它们放在一个单独的文件中。我有一些错误,下面我描述了我的方法。我很感激,如果有人能指导我这里可能出现的问题。
我有一个带有以下树的c ++程序
.
├── main
| └── mymain.cc
├── makefile
├── obj
├── solver
└── data
以及主文件:
#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 . . .
}
以及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)
我想添加一个头文件并将这些函数带到另一个文件中。我修改过的树:
.
├── main
| └── mymain.cc
├── inc
| └── DataHandler.hpp
├── src
| └── DataHandler.cpp
├── makefile
├── obj
├── solver
└── data
我修改后的主要内容:
#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);
}
我的新标头文件:
#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
和相应的cpp文件:
#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 . . .
}
我正在修改makefile,如下所示
# 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)
但是,我收到以下错误。
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)
有人可以就此发生反馈给我一个反馈吗?
答案 0 :(得分:1)
您忘记将dependents文件夹添加到编译器的包含路径中。将-I $(dependents_folder_name)添加到编译规则