0

I have the following structure:

  • bin/ (This is where the final executable will be)
  • build/ (this is where I want the .o files to be)
  • Lexer/
    • src/ (this is where lexer .cpp files are located and where the .o of the lexer are generated)
  • Logger/
    • src/ (this is where logger .cpp files are located and where the .o of the logger are generated)
  • main.cpp (It contains the main function + includes from Lexer and Logger)
  • Makefile

Current makefile:

CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -flto -Ofast
BIN = bin

# Dependency list for each layered tier
DEP_LOGGER = Logger/src/logger.o
DEP_LEXER = Lexer/src/tokens.o Lexer/src/lexer.o

# Nuua dependencies
DEPENDENCIES = nuua.o $(DEP_LOGGER) $(DEP_LEXER)

ifeq ($(OS),Windows_NT)
EXECUTABLE  := nuua.exe
else
EXECUTABLE  := nuua
endif

# Main entry point
all: $(BIN)/$(EXECUTABLE)

# Build the nuua programming language
$(BIN)/$(EXECUTABLE): $(DEPENDENCIES)
    $(CXX) $(CXXFLAGS) -o $@ $^

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    rm build/*.o

.PHONY: push
push:
    git push
    git subtree push --prefix=Lexer Lexer master
    git subtree push --prefix=Logger Logger master

So here's the trick: Is there a way for me to automatically import all files inside Lexer/src/ and Logger/src/ and compile all .o into the build/ directory rather than on the same folder as their respective .cpp file? Of course I don't want to define each .cpp and .o individually. I want freedom if I decide to place a new .cpp to automatically detect it and compile it.

The .o in the build/ directory can have the same name as their respective .cpp file. No .cpp will have the same name, even if they are in diferent folders.

You may also take a look at the repository here: https://github.com/nuua-io/Nuua

EDIT

The closest I could get is the following solution but requires build/MODULE folder.

# Configuration
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -flto -Ofast
BIN = bin
BUILD = build

# Dependency list for each layered tier
MODULES = Logger Lexer

# Objects required to build nuua
OBJS = $(BUILD)/nuua.o $(foreach module,$(MODULES),$(patsubst $(module)/src/%.cpp,$(BUILD)/$(module)/%.o,$(wildcard $(module)/src/*.cpp)))

ifeq ($(OS),Windows_NT)
EXECUTABLE  := nuua.exe
else
EXECUTABLE  := nuua
endif

# Main entry point
all: $(BIN)/$(EXECUTABLE)

# Build the nuua programming language
$(BIN)/$(EXECUTABLE): $(OBJS)
    $(CXX) $(CXXFLAGS) -o $@ $^

build/nuua.o: nuua.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@
build/Lexer/%.o: Lexer/src/%.cpp Lexer/include/%.hpp
    $(CXX) $(CXXFLAGS) -c $< -o $@
build/Logger/%.o: Logger/src/%.cpp Logger/include/%.hpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    rm build/*.o

.PHONY: push
push:
    git push
    git subtree push --prefix=Lexer Lexer master
    git subtree push --prefix=Logger Logger master
Erik Campobadal
  • 867
  • 9
  • 14
  • *"No .cpp will have the same name, even if they are in diferent folders."* - this a very bold assumption – user7860670 Nov 17 '18 at 16:05
  • I am the only one working on this project so I can tell it will. The old structure had everything inside a single /src folder. Now, with this new structure everything is encapsulated (making a correct layered system). The current makefile works, the only problem is that it compiles all .o on the same folder as their .cpp, so they are everywhere rather than a single build folder – Erik Campobadal Nov 17 '18 at 16:08

0 Answers0