AstralHex
AstralHex

Reputation: 21

Incremental compilation using Makefile

I was compiling all my sources each time for each build which is a waste of time. So I tried to implement incremental compilation which means only those .obj files will be generated which are changed in the source and then the .obj files will compiled to a .exe file. But I got errors when I tried to run my updated code.

My current code is

# Gather all the source files manually
SRC = $(wildcard ../../src/*.cpp ../../src/*/*.cpp ../../src/*/*/*.cpp ../../src/*/*/*/*.cpp)
VENDORS = $(wildcard ../include/glad/glad.c ../include/im3d/im3d.cpp ../include/imgui/imgui.cpp ../include/imgui/imgui_draw.cpp ../include/imgui/imgui_tables.cpp ../include/imgui/imgui_widgets.cpp ../include/imgui/imgui_impl_glfw.cpp ../include/imgui/imgui_impl_opengl3.cpp)

# Object files (stored in ../../build directory)
BUILD_DIR = ../../SIN2024/x64/Debug
OBJ = $(SRC:../../src/%.cpp=$(BUILD_DIR)/%.obj) $(VENDORS:../include/%.cpp=$(BUILD_DIR)/%.obj)

# Include and library directories
INCLUDE = -I../include -I../../src
LIBS = -L../lib -lglfw3dll -lassimp

# Compiler and linker flags
CXX = g++
CXXFLAGS = -g --std=c++20 $(INCLUDE)

# Rule to generate object files from source files (to ../../build directory)
$(BUILD_DIR)/%.obj: ../../src/%.cpp
    @mkdir -p $(BUILD_DIR)
    $(CXX) $(CXXFLAGS) -c "$<" -o "$@"

# Main build target
all: main

# Rule to link the final executable
main: $(OBJ)
    $(CXX) -o main $(OBJ) $(LIBS)

# Clean up object files and the executable
clean:
    rm -rf $(BUILD_DIR) main

I expected that the sources will be generated to .obj files and will be stored in the ROOT_DIR/build directory, then these will be generated to the main.exe or the executable. But this fell to an error like this

 *  Executing task: mingw32-make 

mingw32-make: *** No rule to make target '../../SIN2024/x64/Debug/backend/backend.obj', needed by 'main'.  Stop.

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command mingw32-make" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Note that my project structure in the most basic form looks like this

ROOT--
     |---src
     |     |...
     |     |backend/backend.cpp
     |     |...
     |---vendor
     |     |---bin
     |     |     |Makefile
     |     |     |main.exe
     |     |     |...
     |     |---include
     |     |     |...
     |     |---lib
     |     |     |...
     |---build
     |     |...
     |---res
     |     |...

What is the solution or how can I fix the problem?

Upvotes: 2

Views: 85

Answers (0)

Related Questions