I want to make my main and test programs with makefile.
I tried to run this make command but it compiles just the library_management
:
# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++11 -Iinclude
# Directories
SRC_DIR = src
INCLUDE_DIR = include
TEST_DIR = tests
# Executables
MAIN_EXEC = library_management
TEST_EXEC = library_test
# Source files
MAIN_SRC = $(SRC_DIR)/main.cpp $(SRC_DIR)/Book.cpp $(SRC_DIR)/Library.cpp
TEST_SRC = $(TEST_DIR)/LibraryTest.cpp $(SRC_DIR)/Book.cpp $(SRC_DIR)/Library.cpp
# Build main program
$(MAIN_EXEC): $(MAIN_SRC)
$(CXX) $(CXXFLAGS) -o $(MAIN_EXEC) $(MAIN_SRC)
# Build test program
$(TEST_EXEC): $(TEST_SRC)
$(CXX) $(CXXFLAGS) -o $(TEST_EXEC) $(TEST_SRC)
I found a way to make each project with other command. but then I eed to run make
and make test
commands for making both of the projects.
# Default target: build only the main program
all: $(MAIN_EXEC)
# Build main program
$(MAIN_EXEC): $(MAIN_SRC)
$(CXX) $(CXXFLAGS) -o $(MAIN_EXEC) $(MAIN_SRC)
# Build test program
test: $(TEST_EXEC)
$(TEST_EXEC): $(TEST_SRC)
$(CXX) $(CXXFLAGS) -o $(TEST_EXEC) $(TEST_SRC)
Is there an option to make both of the projects in one make command?
Thanks!
As @some-programmer-dude commented, I added both of the projects to the all command and now it is making both of the projects:
all:$(MAIN_EXEC) $(TEST_EXEC)
My makefile looks now like this:
# Compiler and flags
CXX = g++
CXXFLAGS = -std=c++11 -Iinclude
# Directories
SRC_DIR = src
INCLUDE_DIR = include
TEST_DIR = tests
# Executables
MAIN_EXEC = library_management
TEST_EXEC = library_test
# Source files
MAIN_SRC = $(SRC_DIR)/main.cpp $(SRC_DIR)/Book.cpp $(SRC_DIR)/Library.cpp
TEST_SRC = $(TEST_DIR)/LibraryTest.cpp $(SRC_DIR)/Book.cpp $(SRC_DIR)/Library.cpp
# Default target: build main and test programs
all:$(MAIN_EXEC) $(TEST_EXEC)
$(MAIN_EXEC): $(MAIN_SRC)
$(CXX) $(CXXFLAGS) -o $(MAIN_EXEC) $(MAIN_SRC)
$(TEST_EXEC): $(TEST_SRC)
$(CXX) $(CXXFLAGS) -o $(TEST_EXEC) $(TEST_SRC)
# Clean up
clean:
rm -f $(MAIN_EXEC) $(TEST_EXEC)