0

I'm working with a small program in c++ to learn the makefile.

The program has 2 source files (main.cpp and classf.cpp) and one header file (classf.h). All files are included in the project directory which is called "testmake". This is the generated makefile by eclipse on windlows:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0

OBJS =  main.o classf.o
LIBS =
TARGET =    createPddl.exe

$(TARGET):  $(OBJS)
    $(CXX) -o $(TARGET) $(OBJS) $(LIBS)

all:    $(TARGET)

clean:
    rm -f $(OBJS) $(TARGET)

I would like to modify the makefile to accept new sub-directories, e.g, when I add a folder called "testmake/src" and move the file main.cpp inside it, folder called "testmake/csource" and move the classf.cpp inside it, and create a folder called "testmake/cheader" and move the classf.h inside it.

Community
  • 1
  • 1
Waleed A
  • 95
  • 10
  • for the files to compile I guess this question has your answer: http://stackoverflow.com/questions/3774568/makefile-issue-smart-way-to-scan-directory-tree-for-c-files , but I would advise against blanketly adding all the paths that have *.h files to the include folder list – PeterT Jan 22 '16 at 17:15
  • Were do you want the object files (`main.o` and `classf.o`) to go? – Beta Jan 24 '16 at 06:42
  • to a folder called build, and the executable file to folder called bin, and i need to consider the header files that are in folder called include – Waleed A Jan 24 '16 at 16:12

1 Answers1

0

This makefile was genereated automatically by eclipse, and does not accept any changes. There for i have created manually a make file which is working with any c++ project that has a structure as tree.

I actually use this Makefile in general

CC := g++ # This is the main compiler
SRCDIR := src
BUILDDIR := build
TARGETDIR :=bin/
TARGET := pddlcrate
DATADIR := data 
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g # -Wall
#LIB := -pthread -lmongoclient -L lib -lboost_thread-mt -lboost_filesystem-      
mt -lboost_system-mt
INC := -I include

$(TARGET): $(OBJECTS)
    @echo " Linking..."
    @echo " $(CC) $^ -o $(TARGETDIR)$(TARGET) $(LIB)"; $(CC) $^ -o     
$(TARGETDIR)$(TARGET) $(LIB)

$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @mkdir -p $(BUILDDIR)
    @echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -  o $@ $<

clean:
    @echo " Cleaning..."; 
    @echo " $(RM) -r $(BUILDDIR) $(TARGET)"; $(RM) -r $(BUILDDIR) $(TARGET)

for any c++ project with this tree structure

$ tree .
├── Makefile
├── bin
    >exefile
├── include
   > *.h   files
├── obj
   > *.o files
├── src
    >*.cpp
Waleed A
  • 95
  • 10