1

I have the following files in my proj2 directories and need to compile them together to have one executable file.

    proj2/main.cpp
    proj2/model/Player.cpp
    proj2/model/gameBoard.cpp
    proj2/controller/TTTController.cpp
    proj2/Makefile

I'm using the following command inside my makefile, but it is not working.

all:
    g++ /project2_p1/main.cpp /project2_p1/controller/TTTController.cpp          /model/gameBoard.cpp /model/Player.cpp -o ttt
clean:
    -rm ttt

Can anybody help me please.Thank you

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
mabdi
  • 19
  • 1
  • 2

2 Answers2

0

I strongly recommend you start learning make as it is one of the fundamental tools that programmers use. And, if you can learn C++, you can definitely learn make.

In your project you have source files buried in their own subdirectories so in order to find them all you can use the $(shell find...) command. Same with any header files in your project.

By making all: the direct target it gets executed unconditionally and you lose the benefits of using make - only compile when you change something.

Having said that the basic template I am providing here could be improved to recompile only those source files that have changed but that's an exercise for the reader.

I think this should work in your case:

# set non-optional compiler flags here
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic-errors

# set non-optional preprocessor flags here
# eg. project specific include directories
CPPFLAGS += 

# find cpp files in subdirectories
SOURCES := $(shell find . -name '*.cpp')

# find headers
HEADERS := $(shell find . -name '*.h')

OUTPUT := ttt

# Everything depends on the output
all: $(OUTPUT)

# The output depends on sources and headers
$(OUTPUT): $(SOURCES) $(HEADERS)
    $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(OUTPUT) $(SOURCES)

clean:
    $(RM) $(OUTPUT)
Galik
  • 47,303
  • 4
  • 80
  • 117
0

thats my minGW project's makefile codes:

hepsi: derle calistir
Nesneler :=  ./lib/Hata.o ./lib/Hatalar.o ./lib/Dugum.o ./lib/ListeGezici.o ./lib/BagilListe.o

    derle:
        g++ -I ./include/ -o ./lib/Hata.o -c ./src/Hata.cpp
        g++ -I ./include/ -o ./lib/Hatalar.o -c ./src/Hatalar.cpp
        g++ -I ./include/ -o ./lib/Dugum.o -c ./src/Dugum.cpp
        g++ -I ./include/ -o ./lib/ListeGezici.o -c ./src/ListeGezici.cpp
        g++ -I ./include/ -o ./lib/BagilListe.o -c ./src/BagilListe.cpp
        g++ -I ./include/ -o ./bin/test $(Nesneler) ./src/test.cpp

    calistir:
        ./bin/test

In your project I think this will work;

    all: compile run
Objects :=  ./lib/Player.o ./lib/gameBoard.o ./lib/TTTController.o 

compile:
    g++ -I ./include/ -o ./lib/Player.o -c ./model/Player.cpp
    g++ -I ./include/ -o ./lib/gameBoard.o -c ./model/gameBoard.cpp
    g++ -I ./include/ -o ./lib/TTTController.o -c .controller/TTTController.cpp
    g++ -I ./include/ -o ./bin/main $(Objects) ./main.cpp

run:
    ./bin/main

lib folder contains .o files. You can chance it if you want. include folder refers your header .h or .hpp files. You can change every one of them according to your headers location.

bin folder contains your .exe file called main.exe. You can change or remove it like that

run: 
   ./main

I hope it'll work.

@Galik has right. if you want to learn C++, you should definitely learn make.

Ebleme
  • 287
  • 2
  • 13