I have a C project, which has the following file structure:
Makefile
src
|-utils
| |--vic.c
| |--vic.h
|-mod
| |--type.c
| |--type.h
|-bb.c
|-bb.h
|-main.c
So, at the root directory I have the actual Makefile
and the src
directory, which includes the source files. Inside the src
directory there are multiple .c
and .h
files along with the main.c
file, and there are other directories which also contain other .c
and .h
files. Please note that the above shown file structure is kept short for brevity. I want to create a Makefile
that will automatically compile everything and generate an executable main
program, and also delete the object files generated during the compilation. Currently, I have something like this:
CC=gcc
CFLAGS=
RM=rm -rf
OUT=main
SRC=src
OBJ=obj
SOURCES=$(wildcard $(SRC)/*.c)
OBJECTS=$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SOURCES))
all: build
build: $(OBJECTS)
$(CC) $(CFLAGS) $^ -o $@
$(RM) $(OBJ)
$(OBJ)/%.o: $(SRC)/%.c
$(CC) $(CFLAGS) -I$(SRC) -c $< -o $@
debug: CFLAGS+=-DDEBUG
debug: build
.PHONY: clean
clean:
$(RM) $(OBJ) $(OUT)
But that doesn't seem to work as expected, as when I issue make
command, it returns the following error message:
Assembler messages: Fatal error: can't create obj/main.o: No such file or directory
Any ideas how to achieve what I want?
Makefile
that dynamically discovers and builds all the sources. This is not beyond GNUmake
's capabilities, but the effort you will spend getting it to work would probably be better spent on solving the problem a different way. – John Bollingerls
command), or at least making a static list of all the subdirectories, or setting up a separate Makefile in each subdirectory and performing a good old recursivemake
. – John Bollinger