top of page

"Make" project simple again – gtest and gmock part 3

Hi and Welcome again!

Topic mainly for developers based on Linux/Embeded, because visual studio make it automatically.

During this post I will try to explain what is make and why we are using it.

So firstly we can make few things inside make like build, prepare, prepare part, create folders or clean. TABULATORS ARE IMPORTANT!

Simple makefile file: “Makefile” – file name need to be like that

 

all:

G++ -Wall -std=c++11 -o MyGame warrior.o archer.o human.o main.o

warrior.o:

g++ -Wall -std=c++11 -o warrior.o warrior.cc

archer.o:

g++ -Wall -std=c++11 -o archer.o archer.cc

human.o:

g++ -Wall -std=c++11 -o human.o human.cc

main.o

g++ -Wall -std=c++11 -o main.o main.cc

 

We need to save it, open console in directort of Makefile and use command make. After all if everything is correct you will have your game in file MyGame. To runt it you should use set is as executable sometimes and play command – “./MyGame”

But it is awful we want to prepare it faster and simplier, so we can make it like that:

Makefile

 

CFLAGS=-Wall -std=c++11

TARGET = out/MyGame

all: warrior.o archer.o human.o main.o

g++ -o $(TARGET) $(CFLAGS) $?

*.o: *.cpp

g++ $(CFLAGS) $<

clean:

rm *.o

 

Remember it was simple you can make it smarter in bigger projects.

variable= set text

$(variable) – using variable

In “all: warrior.o archer.o human.o main.o” we have sign “$?” that mean put there all inside data which mean it prepare from:

g++ -o $(TARGET) $(CFLAGS) $?

To that:

g++ -o $(TARGET) $(CFLAGS) warrior.o archer.o human.o main.o

and inside variables put texts:

G++ -Wall -std=c++11 -o MyGame warrior.o archer.o human.o main.o

As you see we have automated creation of .o files *.o: *.cpp

g++ $(CFLAGS) $<

that mean take dependencies with o like warrior.o use for that warrior.cc and put it into g++ command which change it:

g++ $(CFLAGS) $<

into that:

g++ -Wall -std=c++11 warior.cc

Some times we want to clear data, because we got problems with compilation. Very often we need to use clean for .o files and trash. That’s why we should create clean part.

We use it like:

- make

- make clean

With that automation we can make great code again, so next post will talk more about gTest which is often used in corporations for TDD – test driven development. TDD is method of programming where you firstly write tests and then you are writing code. From my point of view it is totally not effective and it exist only in big companies with tones of money, so you should leant it. It take more time, but it help in maintenance and they pay for it – so do learn it! And have a nice day.

Subscribe me!

Be sure that you read all.

  • Facebook Clean
  • Black Twitter Icon
  • Black Instagram Icon
  • Black YouTube Icon
Recent Posts
bottom of page