I have a Makefile that i put together from sites I read and stuff I learned, and it works.
Which is nice but the fact that I do not understand all of it is bothering me.
makefile (template) :
-include $(OBJ:.o=.d) # The dash silences errors when files don't exist (yet)
install:
install -C -m 0755 $(EXE) $(INSTALL_DIR)
[/CODE]
this is a template file. T use it i copy it to the directory of my current project, replace ... in "# Name your final target, that is, your executable:" section with the executable name and place my main.c file in the src directory.
Questions:
1
in the all section
what does "$^" and "$@" do and how does that work?
The resulting echo is cc -Iinclude -MMD -MP -Wall -c src/serialp.c -o obj/serialp.o , (the c file in src in this case is "serialp.c").
Which is exactly what i wand and if i have multiple .c files it also works
i just dont understand how it works.
2
The same for
what does "$<" and "$@" do and how does it work.
3
How does "%.o" and "%,c" work?
4
What does "|" do?
5
In the "create bin dir and obj dir if they dont exist" section
i know the mkdir command but how does "$@" work.
6
in the clean section
i know the rm command but i don't see where $(RM) is declared and i don't know how the "@" fits in.
7
in the clean section
Well, to be honest I don't understand this line at all.
8
Most of the programs i make (99.9%) are to learn something and not to use. That is why i install them to the home/bin directory, i don't wand to pollute the user/local/bin directory with stuff i don't use en probably forget to delete.
At the moment i make programs which use multiple sub files for logging and presets etc.
What is the best strategy for installing those sub files if i don't wand to pollute my home/bin directory and wand to keep things organised? P.S. i work on a linux system.
It will be highly appreciated if someone can enlighten me.
Greetings Bert.
.
Which is nice but the fact that I do not understand all of it is bothering me.
makefile (template) :
Code:
# List your source and build output directories:
SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin
INSTALL_DIR:= ~/bin
# Name your final target, that is, your executable:
EXE_NAME := ....
EXE := $(BIN_DIR)/$(EXE_NAME)
# List your source files:
SRC := $(wildcard $(SRC_DIR)/*.c)
#From the source files, list the object files:
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
#handle the flags
CPPFLAGS := -Iinclude -MMD -MP # -I is a preprocessor flag, not a compiler flag
CFLAGS := -Wall # some warnings about bad code
LDFLAGS := -Llib # -L is a linker flag
LDLIBS := -lm #Left empty if no libs are needed ,
# sqlite = -Wl,-rpath -Wl,LIBDIR
# libsodium = -lsodium
# ncursus = -lncurses
# raylib = -lraylib
# so make will not think it must create a file or folder named all, clean and install
.PHONY: all clean install
all: $(EXE)
$(EXE): $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
#create bin dir and obj dir if they dont exist
$(BIN_DIR) $(OBJ_DIR):
mkdir -p $@
clean:
[CODE]@$(RM) -rv $(BIN_DIR) $(OBJ_DIR)
-include $(OBJ:.o=.d) # The dash silences errors when files don't exist (yet)
install:
install -C -m 0755 $(EXE) $(INSTALL_DIR)
[/CODE]
this is a template file. T use it i copy it to the directory of my current project, replace ... in "# Name your final target, that is, your executable:" section with the executable name and place my main.c file in the src directory.
Questions:
1
in the all section
Code:
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
The resulting echo is cc -Iinclude -MMD -MP -Wall -c src/serialp.c -o obj/serialp.o , (the c file in src in this case is "serialp.c").
Which is exactly what i wand and if i have multiple .c files it also works
i just dont understand how it works.
2
The same for
Code:
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
3
Code:
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
4
What does "|" do?
5
In the "create bin dir and obj dir if they dont exist" section
Code:
mkdir -p $@
6
in the clean section
Code:
@$(RM) -rv $(BIN_DIR) $(OBJ_DIR)
7
in the clean section
Code:
-include $(OBJ:.o=.d) # The dash silences errors when files don't exist (yet)
8
Most of the programs i make (99.9%) are to learn something and not to use. That is why i install them to the home/bin directory, i don't wand to pollute the user/local/bin directory with stuff i don't use en probably forget to delete.
At the moment i make programs which use multiple sub files for logging and presets etc.
What is the best strategy for installing those sub files if i don't wand to pollute my home/bin directory and wand to keep things organised? P.S. i work on a linux system.
It will be highly appreciated if someone can enlighten me.
Greetings Bert.
.