25

I would like to build a C-project for my microcontroller with the GNU make tool. I would like to do it in a clean way, such that my source code is not cluttered with object files and other stuff after the build. So imagine that I have a project folder, called "myProject" with two folders in it:

- myProject
     |
     |---+ source
     |
     '---+ build

The build folder only contains a makefile. The figure below shows what should happen when I run the GNU make tool:

enter image description here

So GNU make should create an object file for each .c source file it can find in the source folder. The object files should be structured in a directory tree that is similar to the structure in the source folder.

GNU make should also make a .d dependency file (in fact, a dependency file is some sort of makefile itself) for each .c source file. The dependency file is described in the GNU make manual chapter 4.14 "Generating Prerequisites Automatically":

For each source file name.c there is a makefile name.d which lists what files the object file name.o depends on.

From the following Stackoverflow question About the GNU make dependency files *.d, I learned that adding the options -MMD and -MP to the CFLAGS of the GNU gcc compiler can help to automate that.

So now comes the question. Has anyone a sample makefile that performs such out-of-source build? Or some good advices on how to get started?

I'm pretty sure that most people who have written such a makefile, are Linux-people. But the microcontroller project should build also on a Windows machine. Anyway, even if your makefile is Linux-only, it provides a good starting point ;-)

PS: I would like to avoid extra tools like CMake, Autotools, or anything that has to do with an IDE. Just pure GNU make.

I would be very grateful :-)


Updating the dependency files
Please have a look at this question: What is the exact chain of events when GNU make updates the .d files?

6
  • 1
    You're asking for a pretty simple makefile, so I wouldn't be surprised if someone here gave it to you. But generally we provide free help, not free code.
    – Beta
    Commented Aug 18, 2016 at 11:10
  • Yes you are right.
    – K.Mulier
    Commented Aug 18, 2016 at 11:12
  • This kind of simple Makefile would actually be a good match for the new documentation feature, but for a Q/A it's a bit too broad.
    – Tim
    Commented Aug 18, 2016 at 11:17
  • Normally when I ask a question, I already have my code, and try to specify precisely where the problem is. But this time, I don't have a good starting point... probably because I'm so new to makefiles. Can I keep the question? I would be grateful if anybody can push me in the right direction with some simple sample makefile. I promise to add it later on to the documentation part of Stackoverflow :-) (of course with due credit to the actual writer of the makefile)
    – K.Mulier
    Commented Aug 18, 2016 at 11:18
  • 1
    You want your makefile to 1) build things 2) remotely 3) using everything it finds in the source tree 4) with advanced dependency handling 5) on Windows. I advise you to tackle these one at a time, starting with a HelloWorld makefile on Windows. (I'd like to help, but I don't do Windows).
    – Beta
    Commented Aug 18, 2016 at 11:22

4 Answers 4

24

Here's the Makefile I've added to the documentation (currently in review so I'll post it here) :

# Set project directory one level above the Makefile directory. $(CURDIR) is a GNU make variable containing the path to the current working directory
PROJDIR := $(realpath $(CURDIR)/..)
SOURCEDIR := $(PROJDIR)/Sources
BUILDDIR := $(PROJDIR)/Build

# Name of the final executable
TARGET = myApp.exe

# Decide whether the commands will be shown or not
VERBOSE = TRUE

# Create the list of directories
DIRS = Folder0 Folder1 Folder2
SOURCEDIRS = $(foreach dir, $(DIRS), $(addprefix $(SOURCEDIR)/, $(dir)))
TARGETDIRS = $(foreach dir, $(DIRS), $(addprefix $(BUILDDIR)/, $(dir)))

# Generate the GCC includes parameters by adding -I before each source folder
INCLUDES = $(foreach dir, $(SOURCEDIRS), $(addprefix -I, $(dir)))

# Add this list to VPATH, the place make will look for the source files
VPATH = $(SOURCEDIRS)

# Create a list of *.c sources in DIRS
SOURCES = $(foreach dir,$(SOURCEDIRS),$(wildcard $(dir)/*.c))

# Define objects for all sources
OBJS := $(subst $(SOURCEDIR),$(BUILDDIR),$(SOURCES:.c=.o))

# Define dependencies files for all objects
DEPS = $(OBJS:.o=.d)

# Name the compiler
CC = gcc

# OS specific part
ifeq ($(OS),Windows_NT)
    RM = del /F /Q 
    RMDIR = -RMDIR /S /Q
    MKDIR = -mkdir
    ERRIGNORE = 2>NUL || true
    SEP=\\
else
    RM = rm -rf 
    RMDIR = rm -rf 
    MKDIR = mkdir -p
    ERRIGNORE = 2>/dev/null
    SEP=/
endif

# Remove space after separator
PSEP = $(strip $(SEP))

# Hide or not the calls depending of VERBOSE
ifeq ($(VERBOSE),TRUE)
    HIDE =  
else
    HIDE = @
endif

# Define the function that will generate each rule
define generateRules
$(1)/%.o: %.c
    @echo Building $$@
    $(HIDE)$(CC) -c $$(INCLUDES) -o $$(subst /,$$(PSEP),$$@) $$(subst /,$$(PSEP),$$<) -MMD
endef

# Indicate to make which targets are not files
.PHONY: all clean directories 

all: directories $(TARGET)

$(TARGET): $(OBJS)
    $(HIDE)echo Linking $@
    $(HIDE)$(CC) $(OBJS) -o $(TARGET)

# Include dependencies
-include $(DEPS)

# Generate rules
$(foreach targetdir, $(TARGETDIRS), $(eval $(call generateRules, $(targetdir))))

directories: 
    $(HIDE)$(MKDIR) $(subst /,$(PSEP),$(TARGETDIRS)) $(ERRIGNORE)

# Remove all objects, dependencies and executable files generated during the build
clean:
    $(HIDE)$(RMDIR) $(subst /,$(PSEP),$(TARGETDIRS)) $(ERRIGNORE)
    $(HIDE)$(RM) $(TARGET) $(ERRIGNORE)
    @echo Cleaning done ! 

Main features

  • Automatic detection of C sources in specified folders
  • Multiple source folders
  • Multiple corresponding target folders for object and dependency files
  • Automatic rule generation for each target folder
  • Creation of target folders when they don't exist
  • Dependency management with gcc : Build only what is necessary
  • Works on Unix and DOS systems
  • Written for GNU Make

How to use this Makefile

To adapt this Makefile to your project you have to :

  1. Change the TARGET variable to match your target name
  2. Change the name of the Sources and Build folders in SOURCEDIR and BUILDDIR
  3. Change the verbosity level of the Makefile in the Makefile itself or in make call (make all VERBOSE=FALSE)
  4. Change the name of the folders in DIRS to match your sources and build folders
  5. If required, change the compiler and the flags

In this Makefile Folder0, Folder1 and Folder2 are the equivalent to your FolderA, FolderB and FolderC.

Note that I have not had the opportunity to test it on a Unix system at the moment but it works correctly on Windows.


Explanation of a few tricky parts :

Ignoring Windows mkdir errors

ERRIGNORE = 2>NUL || true

This has two effects : The first one, 2>NUL is to redirect the error output to NUL, so as it does not comes in the console.

The second one, || true prevents the command from rising the error level. This is Windows stuff unrelated with the Makefile, it's here because Windows' mkdir command rises the error level if we try to create an already-existing folder, whereas we don't really care, if it does exist that's fine. The common solution is to use the if not exist structure, but that's not UNIX-compatible so even if it's tricky, I consider my solution more clear.


Creation of OBJS containing all object files with their correct path

OBJS := $(subst $(SOURCEDIR),$(BUILDDIR),$(SOURCES:.c=.o))

Here we want OBJS to contain all the object files with their paths, and we already have SOURCES which contains all the source files with their paths. $(SOURCES:.c=.o) changes *.c in *.o for all sources, but the path is still the one of the sources. $(subst $(SOURCEDIR),$(BUILDDIR), ...) will simply subtract the whole source path with the build path, so we finally have a variable that contains the .o files with their paths.


Dealing with Windows and Unix-style path separators

SEP=\\
SEP = /
PSEP = $(strip $(SEP))

This only exist to allow the Makefile to work on Unix and Windows, since Windows uses backslashes in path whereas everyone else uses slashes.

SEP=\\ Here the double backslash is used to escape the backslash character, which make usually treats as an "ignore newline character" to allow writing on multiple lines.

PSEP = $(strip $(SEP)) This will remove the space char of the SEP variable, which has been added automatically.


Automatic generation of rules for each target folder

define generateRules
$(1)/%.o: %.c
    @echo Building $$@
    $(HIDE)$(CC) -c $$(INCLUDES) -o $$(subst /,$$(PSEP),$$@)   $$(subst /,$$(PSEP),$$<) -MMD
endef

That's maybe the trick that is the most related with your usecase. It's a rule template that can be generated with $(eval $(call generateRules, param)) where param is what you can find in the template as $(1). This will basically fill the Makefile with rules like this for each target folder :

path/to/target/%.o: %.c
    @echo Building $@
    $(HIDE)$(CC) -c $(INCLUDES) -o $(subst /,$(PSEP),$@)   $(subst /,$(PSEP),$<) -MMD
7
  • Thank you so much Tim! Your makefile is very elegant and well documented. Your reply is a very big help for me. I will test it this evening. Thank you, thank you, thank you :-)
    – K.Mulier
    Commented Aug 19, 2016 at 8:34
  • I'm glad you like it, actually I've had a lot of pleasure doing it. It was a bit challenging and it's related to the third rule of Makefiles : Life is simplest if the targets are built in the current working directory. But who wants a simple life ? ;)
    – Tim
    Commented Aug 19, 2016 at 8:41
  • It looks great ! I've added explanations at the end of the answer to ease a bit the understanding of this Makefile. For the reference you can simply link to this stackoverflow Q/A, it's fine for me. By the way most of the tricks I use have been found on others SO Q/A or on other sites.
    – Tim
    Commented Aug 19, 2016 at 9:17
  • @TimF: This example is really helpful. If Windows support is not required, what would $(HIDE)$(CC) -c $$(INCLUDES) -o $$(subst /,$$(PSEP),$$@) $$(subst /,$$(PSEP),$$<) -MMD simplify to? (i.e. if we remove PSEP?)
    – DavidA
    Commented Sep 28, 2016 at 13:41
  • @DavidA I'm glad it helps ! If you only use WIndows then you don't need a Makefile variable, so you can simply drop the subst function and keep only the content : $(HIDE)$(CC) -c $$(INCLUDES) -o $$@ $$< -MMD . The subst is here to replace the slash by a slash or a backslash depending on your OS.
    – Tim
    Commented Sep 28, 2016 at 13:45
3

Here's a basic one I use all the time, it's pretty much a skeleton as it is but works perfectly fine for simple projects. For more complex projects it certainly needs to be adapted, but I always use this one as a starting point.

APP=app

SRC_DIR=src
INC_DIR=inc
OBJ_DIR=obj
BIN_DIR=bin

CC=gcc
LD=gcc
CFLAGS=-O2 -c -Wall -pedantic -ansi
LFLGAS=
DFLAGS=-g3 -O0 -DDEBUG
INCFLAGS=-I$(INC_DIR)

SOURCES=$(wildcard $(SRC_DIR)/*.c)
HEADERS=$(wildcard $(INC_DIR)/*.h)
OBJECTS=$(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
DEPENDS=$(OBJ_DIR)/.depends


.PHONY: all
all: $(BIN_DIR)/$(APP)

.PHONY: debug
debug: CFLAGS+=$(DFLAGS)
debug: all


$(BIN_DIR)/$(APP): $(OBJECTS) | $(BIN_DIR)
    $(LD) $(LFLGAS) -o $@ $^

$(OBJ_DIR)/%.o: | $(OBJ_DIR)
    $(CC) $(CFLAGS) $(INCFLAGS) -o $@ $<

$(DEPENDS): $(SOURCES) | $(OBJ_DIR)
    $(CC) $(INCFLAGS) -MM $(SOURCES) | sed -e 's!^!$(OBJ_DIR)/!' >$@

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPENDS)
endif


$(BIN_DIR):
    mkdir -p $@
$(OBJ_DIR):
    mkdir -p $@

.PHONY: clean
clean:
    rm -rf $(BIN_DIR) $(OBJ_DIR)
0
2

This fairly minimal makefile should do the trick:

VPATH = ../source
OBJS = FolderA/fileA1.o FolderA/fileA2.o FolderB/fileB1.o
CPPFLAGS = -MMD -MP

all: init myProgram

myProgram: $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

.PHONY: all init

init:
        mkdir -p FolderA
        mkdir -p FolderB

-include $(OBJS:%.o=%.d)

The main tricky part is ensuring that FolderA and FolderB exist in the build directory bfore trying to run the compiler that will write into them. The above code will work sequential for builds, but might fail with -j2 the first time it is run, as the compiler in one thread might try to open an output file before the other thread creates the directory. Its also somewhat unclean. Usually with GNU tools you have a configure script that will create those directories (and the makefile) for you before you even try to run make. autoconf and automake can build that for you.

An alternate way that should work for parallel builds would be to redefine the standard rule for compiling C files:

VPATH = ../source
OBJS = FolderA/fileA1.o FolderA/fileA2.o FolderB/fileB1.o
CPPFLAGS = -MMD -MP

myProgram: $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

%.o: %.c
        mkdir -p $(dir $@)
        $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

-include $(OBJS:%.o=%.d)

Which has the disadvantage that you'll also need to redefine the builtin rules for any other kind of sourcefile you want to compile

5
  • Waw, thank you so much! You say that it will not work with -j2 the first time it is run. This option is to parallelize the build, right? I presume it won't work in a parallel build because the folders FolderA and FolderB might not exist when the object files get created. Am I correct?
    – K.Mulier
    Commented Aug 18, 2016 at 16:40
  • How does VPATH handle multiple source files with the same name? Commented Aug 18, 2016 at 17:15
  • 1
    @MaximEgorushkin: You can't have mulitple files with the same name in the same directory. The subdirectory (under source) is part of the name, so the same base file name in different subdirectories is fine. The object and dependency files will have the same name (including subdir name) with the extension changed. So you end up with the same directory structure under build as under source
    – Chris Dodd
    Commented Aug 18, 2016 at 18:24
  • 1
    The -j2 will (possibly) fail if it tries to build the first source file at the same time as it tries to run the init action to create the directories -- if something happens that makes mkdir run much slower than cc, the compiler might try to open the output file before the directory exists.
    – Chris Dodd
    Commented Aug 18, 2016 at 18:26
  • Hi @ChrisDodd , I just posted another makefile question on Stackoverflow. The question is about advanced variable inheritance. stackoverflow.com/questions/39036774/…
    – K.Mulier
    Commented Aug 19, 2016 at 10:44
1

I would avoid manipulating Makefile directly, and use CMake instead. Just describe your source files in CMakeLists.txt, as below:

Create file MyProject/source/CMakeLists.txt containing;

project(myProject)
add_executable(myExec FolderA/fileA1.c FolderA/fileA2.c FolderB/fileB1.c)

Under MyProject/build, run

cmake ../source/

You'll get a Makefile now. To build, under the same build/ directory,

make

You may also want to switch to a lightning fast build tool, ninja, simply by adding a switch as following.

cmake -GNinja ..
ninja
2
  • 1
    Welcome to Stack Overflow! This is really a comment, not an answer. With a bit more rep, you will be able to post comments. Commented Aug 23, 2016 at 1:31
  • 1
    @manetsus Thanks for your comment. I've just modified my answer.
    – exavolt
    Commented Aug 24, 2016 at 7:21

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.