Adding header file and related modification to the makefile - c++

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?

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

Related

Using makefile to link cimgui to project?

I have a c project and I'm trying to link the C binding for imgui to it using Makefiles but I'm having some trouble. I've already generated the .dll using cmake in a build folder, these are the contents of it:
C:\path\to\cimgui\build:
-CMakefiles
-cimgui.dll
-cmake_install.txt
-CMakeCache.txt
-libcimgui.dll.a
-Makefile
and this is my makefile
PROJ := proj
TARGET := $(PROJ)
OBJ := sub.o main.o
INCLUDE_PATH := -IC:\path\to\SDL2 -IC:\path\to\cimgui
LIB_PATH := -LC:\path\to\SDL2\lib -LC:\path\to\cimgui\build
LINKER_FLAGS := -lSDL2 -lopengl32 -lcimgui -lcimgui.dll
CFLAGS := $(INCLUDE_PATH)
.PHONY : build clean
build : $(TARGET).exe
$(TARGET).exe: $(OBJ)
gcc $^ $(LIB_PATH) $(LINKER_FLAGS) -o $#
sub.o: sub.c sub.h
gcc -c $(CFLAGS) $< -o $#
main.o: main.c sub.h
$(CC) $(CFLAGS) $< -o $#
clean :
#rm -fv *.o
#rm -fv *.exe
and this is my main.c project
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_opengl.h>
int main(int argc, char ** argv)
{
return 0;
}
This is the error I get when i try to compile my makefile:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lcimgui.dll

Problem in compilation of MINPACK library with CUDA code

I need to solve a nonlinear system of equations and I found the MINPACK library to do the job.
But I need the code to work with CUDA and I'm not able to compile the code.
I'm facing this problem:
nvcc -arch sm_61 -Iinc -I/usr/local/include obj/main.o -o oi -L/usr/local/lib64 -lcuminpack
nvlink error : Undefined reference to 'dpmpar' in 'obj/main.o'
make: *** [makefile:55: oi] Erro 255
My main file is:
main.cu
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define __cminpack_double__
#include <cminpack-1/cminpack.h>
#define real __cminpack_real__
typedef struct{
real fnorm1, fnorm2;
int info;
real solution[2];
} ResultType;
__cminpack_attr__ int fcn(void *p, int n, const real *x, real *fvec, int iflag){
--fvec;
--x;
fvec[0] = x[0] + x[0]*x[1] - 4.0;
fvec[1] = x[0] + x[1] -3.0;
return 0;
}
__global__ void SolvingSystem(ResultType *pResults){
int info;
real fnorm1, fnorm2, tol;
int n = 2;
real fvec[2];
real x[2];
const int lwa = (n*(3*n + 13))/2;
real wa[50];
tol = sqrt(dpmpar(1));
x[0] = 1.98;
x[1] = 1.02;
printf("Initial Guess: %g, %g\n", x[0], x[1]);
}
int main(int argc, char const *argv[]){
cudaSetDevice(0);
ResultType *reuslt_GPU, *reuslt_CPU;
cudaMalloc((void**)&reuslt_GPU, sizeof(ResultType));
cudaMallocHost((void**)&reuslt_CPU, sizeof(ResultType));
printf("Solving System...\n");
dim3 grid(1, 1, 1);
dim3 block(1, 1, 1);
SolvingSystem<<< grid, block>>>(reuslt_GPU);
cudaDeviceSynchronize();
printf("Done!\n");
return 0;
}
And my makefile is:
makefile
# MakeFile
# Program Name
EXE = oi
# Compilers
NVCC := nvcc
ALL_LIBRARIES := cuminpack
LIBDIR := /usr/local/lib64
# Directories
SRCDIR := src
OBJDIR := obj
INCDIR := inc /usr/local/include
#Flags
NVCCARCHFLAG := -arch sm_61
NVCCFLAGS := -std=c++11
LDFLAGS := $(addprefix -L, $(LIBDIR))
INCLUDES := $(addprefix -I, $(INCDIR))
LIBRARIES := $(addprefix -l, $(ALL_LIBRARIES))
LIBRARIES +=
ALL_CPFLAGS := -MMD
ALL_CPFLAGS += $(NVCCARCHFLAG)
ALL_CPFLAGS += $(NVCCFLAGS)
ALL_LDFLAGS := $(LDFLAGS)
# Files
C_FILES := $(wildcard $(SRCDIR)/*.c)
CU_FILES := $(wildcard $(SRCDIR)/*.cu)
C_OBJ := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(C_FILES))
CU_OBJ := $(patsubst $(SRCDIR)/%.cu, $(OBJDIR)/%.o, $(CU_FILES))
C_DEP := $(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.d, $(C_FILES))
CU_DEP := $(patsubst $(SRCDIR)/%.cu, $(OBJDIR)/%.d, $(CU_FILES))
SRC := $(C_FILES) $(CU_FILES)
OBJ := $(C_OBJ) $(CU_OBJ)
DEP := $(C_DEP) $(CU_DEP)
COMPILE.c := $(NVCC) -MMD -g $(INCLUDES) -c
COMPILE.cu := $(NVCC) $(ALL_CPFLAGS) $(INCLUDES) -dc
.PHONY: all clean
all:$(EXE)
$(EXE): $(OBJ)
$(NVCC) $(NVCCARCHFLAG) $(INCLUDES) $^ -o $# $(ALL_LDFLAGS) $(LIBRARIES)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(COMPILE.c) $< -o $#
$(OBJDIR)/%.o: $(SRCDIR)/%.cu | $(OBJDIR)
$(COMPILE.cu) $< -o $#
$(OBJDIR):
#mkdir -p $#
# Cleaning up
clean:
#echo Cleaning up...
#rm -f -r $(OBJDIR)
#rm $(EXE)
# Including dependency
-include $(DEP)
The main file needs to be finished and that's the reason to some unused variables.
I just can't figure out what is causing this error, I think the problem is in cuminpack library, but I don't now how to fix it.
I'm using cminpack-1.3.8 from this website: http://devernay.free.fr/hacks/cminpack/
I installed the library using cmake then make and make install. Then I went to source folder and did make cuda. This last command generated libcuminpack.a file in cuda directory, then I copied this .a file to /usr/local/lib64 where cmake installed the other libraries files.
If anyone could help me I would be very thankful
EDIT
Files from the cminpack library
cminpack.h
...
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
...
#if defined(__CUDA_ARCH__) || defined(__CUDACC__)
#define __cminpack_attr__ __device__
#ifndef __cminpack_real__
#define __cminpack_float__
#define __cminpack_real__ float
#endif
#define __cminpack_type_fcn_nn__ __cminpack_attr__ int fcn_nn
#define __cminpack_type_fcnder_nn__ __cminpack_attr__ int fcnder_nn
#define __cminpack_type_fcn_mn__ __cminpack_attr__ int fcn_mn
#define __cminpack_type_fcnder_mn__ __cminpack_attr__ int fcnder_mn
#define __cminpack_type_fcnderstr_mn__ __cminpack_attr__ int fcnderstr_mn
#define __cminpack_decl_fcn_nn__
#define __cminpack_decl_fcnder_nn__
#define __cminpack_decl_fcn_mn__
#define __cminpack_decl_fcnder_mn__
#define __cminpack_decl_fcnderstr_mn__
#define __cminpack_param_fcn_nn__
#define __cminpack_param_fcnder_nn__
#define __cminpack_param_fcn_mn__
#define __cminpack_param_fcnder_mn__
#define __cminpack_param_fcnderstr_mn__
...
__cminpack_attr__
__cminpack_real__ CMINPACK_EXPORT __cminpack_func__(dpmpar)( int i );
dpmpar.c
#include "cminpack.h"
#include <float.h>
#include "cminpackP.h"
#define DPMPAR(type,X) _DPMPAR(type,X)
#define _DPMPAR(type,X) type ## _ ## X
__cminpack_attr__
real __cminpack_func__(dpmpar)(int i)
{
/* ********** */
/* Function dpmpar */
...
Based on talonmies's comment I was able to compile the code defining the __device__ function dpmpar in main.cu file.
Then I found this post https://forums.developer.nvidia.com/t/external-calls-to-device-functions/17737 where the guy answered that CUDA doesn't have a linker on the device side, so it's not possible to call a __device__ function from a different .cu file.
With this in mind, I copied only the files that I needed from the library and made some modifications, like changing __cminpack_attr__ to __device__ and I rewrote the header file too. So basically I made my own device library (thank's again to talonmies's).
The last thing I did was move all this new files to the same folder where my main.cu and my makefile lives and I rewrote the makefile.
Now the code compile fine!!

Linker errors when trying to statically linking Boost

I am trying to include a Boost library in my program, but I am having difficulties statically linking my program. I get a bunch of linker errors even though I have added -L/usr/include/boost/ -lboost_filesystem to my makefile.
E.g., during compilation I get undefined reference to boost::iostreams::detail::gzip_footer::reset()'
My version of Boost is 1.61.0.2, I am running Ubuntu 16.10 (64 bit) and gcc version 6.2.0 20161005. My boost libraries such as accumulators, algorithms, ... are located in /usr/include/boost, so my makefile looks like this:
CXX = g++
CXXFLAGS = -static -std=c++11 -Wall
LDFLAGS = -L/usr/include/boost/ -lboost_filesystem
DEPFLAGS = -MM
SRC_DIR = ./src
OBJ_DIR = ./obj
SRC_EXT = .cpp
OBJ_EXT = .o
TARGET = main
SRCS := $(wildcard $(SRC_DIR)/*$(SRC_EXT))
OBJS := $(SRCS:$(SRC_DIR)/%$(SRC_EXT)=$(OBJ_DIR)/%$(OBJ_EXT))
DEP = depend.main
.PHONY: clean all depend
all: $(TARGET)
$(TARGET): $(OBJS)
#echo "-> linking $#"
#$(CXX) $^ $(LDFLAGS) -o $#
$(OBJ_DIR)/%$(OBJ_EXT) : $(SRC_DIR)/%$(SRC_EXT)
#echo "-> compiling $#"
#$(CXX) $(CXXFLAGS) -o $# -c $<
clean:
#echo "removing objects and main file"
#rm -f $(OBJS) $(TARGET) *.out
$(SRC_DIR)/%.$(SRC_EXT):
$(CXX) $(DEPFLAGS) -MT \
"$(subst $(SRC_DIR),$(OBJ_DIR),$(subst $(SRC_EXT),$(OBJ_EXT),$#))" \
$(addprefix ,$#) >> $(DEP);
clear_dependencies:
#echo "-> (re-)building dependencies";
#$(RM) $(DEP)
depend: clear_dependencies $(SRCS)
-include $(DEP)
I'm trying to compile the following file (an example found online)
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
namespace bo = boost::iostreams;
int main()
{
{
std::ofstream ofile("hello.gz", std::ios_base::out | std::ios_base::binary);
bo::filtering_ostream out;
out.push(bo::gzip_compressor());
out.push(ofile);
out << "This is a gz file\n";
}
{
std::ifstream ifile("hello.gz", std::ios_base::in | std::ios_base::binary);
bo::filtering_streambuf<bo::input> in;
in.push(bo::gzip_decompressor());
in.push(ifile);
boost::iostreams::copy(in, std::cout);
}
}
Your program does not use libboost_filesystem at all. The only
boost library it depends on is liboost_iostreams.

Makefile that only recompiles changed objects?

Alright, new user here, and I've got a problem. I'm a new c++ student, and I have no prior experience in this language (before about 3 months ago). My assignment is as follows:
Write a program that declares an array darray of 50 elements of type double. Initialize the array so that the first 25 elements are equal to the square of the index variable, and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.
The program should have two functions: a function, initArray(), which initializes the array elements, and a function, prArray(), which prints the elements.
I have that, it's as follows
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
double initArray();
double prArray(double arrayone[50]);
double * dArray;
int main() {
dArray[50] = initArray();
system("PAUSE");
prArray(dArray);
system("PAUSE");
return 0;
}
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <iostream>
#include <string>
using namespace std;
double prArray(double arraytwo[50])
{
for (int x = 0; x < 50; x++) {
cout << arraytwo[x];
if (x = 9 || 19 || 29 || 39 || 49) {
cout << endl;
}
}
return 0;
}
#include "printArray.h"
#include "initializearray.h"
#include "Main.h"
#include <iostream>
#include <string>
int x = 0;
double arrayone[50];
double initArray()
{
for (x = 0; x < 25; x++) {
arrayone[x] = (x*x);
}
for (x = 25; x <= 50; x++) {
arrayone[x] = (x * 3);
}
return arrayone[50];
}
Now my problem is that the assignment goes on to say
Write a Makefile to compile the program above that minimizes recompiling items upon changes. (e.g., if one function file gets updated, only the necessary file(s) are recompiled.) Include a clean target that removes compiled objects if invoked.
I have a basic makefile:
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=Main.cpp initializeArray.cpp printArray.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=Main
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $#
.cpp.o:
$(CC) $(CFLAGS) $< -o $#
Now, what I need help with is turning this into a makefile that satisfies the assignment conditions - preferably with step-by-step instructions so that I can learn from this.
Modified your Makefile to:
Automatically generate header dependencies.
Re-build and re-link when Makefile changes.
CXX := g++
LD := ${CXX}
CXXFLAGS := -Wall -Wextra -std=gnu++14 -pthread
LDFLAGS := -pthread
exes :=
# Specify EXEs here begin.
Main.SOURCES := Main.cpp initializeArray.cpp printArray.cpp
exes += Main
# Specify EXEs here end.
all: $(exes)
.SECONDEXPANSION:
get_objects = $(patsubst %.cpp,%.o,${${1}.SOURCES})
get_deps = $(patsubst %.cpp,%.d,${${1}.SOURCES})
# Links executables.
${exes} : % : $$(call get_objects,$$*) Makefile
${LD} -o $# $(filter-out Makefile,$^) ${LDFLAGS}
# Compiles C++ and generates dependencies.
%.o : %.cpp Makefile
${CXX} -o $# -c ${CPPFLAGS} ${CXXFLAGS} -MP -MD $<
# Include the dependencies generated on a previous build.
-include $(foreach exe,${exes},$(call get_deps,${exe}))
.PHONY: all

The executable test file is not running the tests

I have created a project:
// func.h :
#ifndef FUNCS_H_
#define FUNCS_H_
int addInts(int i, int j);
#endif /* FUNCS_H_ */
// func.cpp
#include "func.h"
int addInts(int i, int j) {
return i + j;
}
// main.cpp
#include "func.h"
#include <iostream>
int main() {
std::cout << addInts(5, 7) << std::endl;
return 0;
}
//makefile
OBJS := \
main.o \
func.o
CXX := g++
CXX_FLAGS := -Wall -Werror -Wextra -std=c++11
all: main_prj
main_prj: $(OBJS)
$(CXX) $(OBJS) -lm -o main
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXX_FLAGS) $*.cpp -o $*.o
clean:
rm -f main $(OBJS)
And I created also a test (boost test) for that function:
// test/main_test.cpp
#define BOOST_TEST_MODULE main_test
#include <boost/test/included/unit_test.hpp>
//____________________________________________________________________________//
// FIXTURES ...
//____________________________________________________________________________//
// test/addInts/addInts_test.cpp
#include <boost/test/unit_test.hpp>
#include "../../func.h"
BOOST_AUTO_TEST_CASE(addInts_5_7) {
int addInts_5_7 = 5 + 7;
BOOST_CHECK_EQUAL(addInts_5_7, addInts(5, 7));
}
// test/makefile
OBJS_TEST := \
main_test.o \
addInts/addInts_test.o \
../func.o
CXX_TEST := g++
CXX_FLAGS_TEST := -Wall -Werror -Wextra -std=c++11
all_test: main_test_prj
main_test_prj: $(OBJS_TEST)
$(CXX_TEST) $(OBJS_TEST) -lm -o main_test
-include $(OBJS_TEST:.o=.d)
%.o: %.cpp
$(CXX_TEST) -c $(CXX_FLAGS_TEST) $*.cpp -o $*.o
clean_test:
rm -f main_test $(OBJS_TEST)
Now, the make commands work, so they clean all the created files, or create the o files and the executables. When I run the main file, the output is correct: 12 ; but when I run the main_test file, the output is a little strange:
Running 1 test case...
*** No errors detected
I would expect the output with running tests and OK/KO, or pass/not pass... I cannot figure what am I doing wrong. Can anyone help me, please?
You have one test.
The output says that one test was run, and that no errors were found.
This output appears to be as documented.
There is no problem here.
Though, sadly, the documentation on how to change this output is rather lacking…