0

So I have this Makefile

#Select the compiler
CXX?=g++

#INCLUDES (.h) directories, can have as many as we want
CLASSES=classes/

INCLUDES= -I. -Iincludes/ -I$(CLASSES)

#Library stuff
LIB_FLAGS=

#
OTHER_FLAGS= -O3 -lm -lrt -std=gnu++17 -lpthread -fopenmp -lcurl

SOURCES = main.cpp $(CLASSES)/*.cpp 

PROGRAM_NAME=flashbot.bin
PROGRAM_DIR=.
PROGRAM_PATH=$(PROGRAM_DIR)/$(PROGRAM_NAME) 


all:
    +$(CXX) $(SOURCES) $(INCLUDES) $(LIB_FLAGS) $(OTHER_FLAGS) -o $(PROGRAM_PATH)

profile:
    $(CXX) -pg -DNOUSLEEP -D_LARGEFILE64_SOURCE $(SOURCES) $(INCLUDES) $(LIB_FLAGS)  -o $(PROGRAM_PATH).profile  -lm -lrt -std=gnu++17 -lpthread

run:
    ./$(PROGRAM_PATH)
    
clean:
    @rm -r $(PROGRAM_PATH)
    @rm -r $(PROGRAM_PATH).profile
    @rm -r gmon.out

Nothing crazy for sure.

You can notice that in the variable SOURCES I include *.cpp. It's a bit drastic, but it's simple and it works.

The problem is my program is not that big (around 10 classes, and not so many lines of code), but it already takes an annoying 20 to 30 seconds to compile.

So, I am wondering how to make my Makefile able to compile things in parallel. You know so that I can use make -j 8 for instance.

I am guessing that I have divide the compiling process into many steps, those steps being independent enough from one another so that they can be ran in parallel at compile time.

Thus I am wondering: what is the correct way to have my code to compile in parallel AND so that I do not have to list manually ALL the cpp files one by one?

In other words, is there a way to create a Makefile that allows parallel compiling without having to write every target manually?

Thank you in advance for your help!

MrJay42
  • 77
  • 2
  • 6

0 Answers0