0

I am still newish to makefile patterns. I have been working on a project based on a template makefile and project structure, however I have a good sense of what all the makefile instructions are doing.

I would like to modify this so that all the .c files in /src/mains, each with a different main call gets compiled to its own executable.

The original template had no /src/mains folder, and only one file in the /src could contain a main function call.

First the project structure

root
|
|------ Makefile 
|
|------ build
|        |
|        |--- apps
|        |      |--- prog
|        |      |--- hello
|        |
|        |---- objects
|               |--- *.o
|
|----- include
|        |--- *.h
|                
|------ src
         |--- *.c // for each .h (no main calls)
         |--- mains
                |--- prog.c (call to main)
                |--- hello.c (call to different main)

Current Makefile

Without manually writing new linking rules for each make, I found no success. I am just going to leave the cleaner, unmodified makefile here, I'd like to change this so that it automatically builds all the files in mains to /build/apps

As of right now it makes an executable called prog in /build/apps, using a main function call from within /src

CXX      := gcc
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS  := -lm
BUILD    := ./build
OBJ_DIR  := $(BUILD)/objects
APP_DIR  := $(BUILD)/apps
TARGET   := prog                 # OP: this probably needs to become a wildcard, right?
INCLUDE  := -Iinclude/
SRC      := $(wildcard src/*.c)
OFLAGS   := -O1 -flto

OBJECTS  := $(SRC:%.c=$(OBJ_DIR)/%.o)
DEPENDENCIES := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.c
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@ $(OFLAGS)

$(APP_DIR)/$(TARGET): $(OBJECTS)
    @mkdir -p $(@D)
    $(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

build:
    @mkdir -p $(APP_DIR)
    @mkdir -p $(OBJ_DIR)

clean:
    -@rm -rvf $(OBJ_DIR)/*
    -@rm -rvf $(APP_DIR)/*

If you could modify the Makefile outright that would be great, I would also appreciate any explanation so I don't need a template next time.

Also more generally, How do you like to structure your projects?

Thanks!

1
  • Where do you want to put prog.o and hello.o? In build/objects with the rest of the object files?
    – Beta
    May 7, 2022 at 4:41

1 Answer 1

1

Make has no knowledge of programming languages at all. It is simply a tool that receives your set of rules and dependency trees.

So create a target that has all your programs as dependencies. Add it to the .PHONY target, as it is not a real file.

Also write your rules so that each program can be built independently from the others. You can have synergies, if your programs are built with common modules.

The options -p, -n and -d are helpful to debug the Makefile. Reading the manual and tutorials is a must, as always.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.