Including Multiple Classes/ .o files with MinGW Tool in C++

Viewed 214

I want to compile 2 classes into .o file and include them in Test.exe file created from avl.cpp main file.

I use the MinGW tool while doing this, but Nothing happens when I type the command mingw32-make into CMD. When I did this with only 1 class, there was no problem. The content of the makefile file is as follows:

all: compile execute

compile:
    g++ -I ./include/ -o ./lib/AVLClass.o -c ./src/AVLClass.cpp
    g++ -I ./include/ -o ./lib/PersonsClass.o -c ./src/PersonsClass.cpp
    g++ -I ./include/ -o ./bin/Test ./lib/PersonsClass.o  ./lib/AVLClass.o ./src/avl.cpp
    
execute:
    ./bin/Test
1 Answers

Makefile uses dependancies to determine what gets built in which order. So the line execute: should really be execute: compile to tell make to do the execute step after the compile step completed.

But you should really split compilation an linking into steps to use the dependancy resolving qualities of make.

Here's how I would do it:

BINEXT=.exe
CXX=g++
MKDIR=mkdir -p
RM=rm -f

all: bin/Test$(BINEXT) test

lib/%.o: src/%.cpp
    $(MKDIR) lib
    $(CXX) -c -o $@ $^

bin/Test$(BINEXT): lib/AVLClass.o lib/PersonsClass.o lib/avl.o
    $(MKDIR) bin
    $(CXX) -o $@ $^

test: bin/Test$(BINEXT)
    bin/Test$(BINEXT)

clean:
    $(RM) lib/*.o bin/Test$(BINEXT)

Note that the indents must be tabs, not spaces.

Actually I wouldn't add test to the all target. It's better to leave it up to the user if they want to run make test.

Related