explicaiton des makefiles presque fini

This commit is contained in:
Hugo LAMY
2019-11-15 19:39:02 +01:00
parent 28e08d8238
commit 848931d537
9 changed files with 289 additions and 50 deletions

BIN
.DS_Store vendored

Binary file not shown.

316
Makefile
View File

@@ -80,15 +80,14 @@ SRCS = ft_atoi.c \
ODIR = ./builds ODIR = ./builds
# - "addprefix" is a built-in expansion function used by # - $(SRCS:%.c=$(ODIR)/%.o) is a built-in function called a
# makefile to perform a transformation on file names, in # "substitute reference", an abbreviation for the
# this case we want to put all the .o files in a # expansion function "patsubst" :
# subdirectory named "build" # $(patsubst %.c,$(ODIR)/%.o,$(SRCS))
# - $(SRCS:.c=.o) is a "substitute reference", an # - % match everything, the value of the first % in
# abbreviation for the expansion function "patsubst" : # "replacement" is replaced by the text matched by the
# $(patsubst %.c,%.o,$(SRCS)) # first one in "pattern", it only works for the firsts
OBJ = $(addprefix $(ODIR)/, $(SRCS:.c=.o)) OBJ = $(SRCS:%.c=$(ODIR)/%.o)
# - - - - - - - - - - - # # - - - - - - - - - - - #
# rules to execute # # rules to execute #
@@ -468,76 +467,307 @@ re: fclean all
# makefile basique # makefile basique
# - - - - - - - - - # - - - - - - - - -
# exemple d'un makefilede basic qui sert a creer un # exemple for a basic makefile :
# executable pour un programme :
# #
# [archtecture] # [architecture]
# # main.c # # main.c
# # function01.c # # function01.c
# # function02.c # # function02.c
# # header.h # # header.h
# # libtest.a
# # Makefile # # Makefile
# #
# [Makefile] # [Makefile]
# # # DECLARATION VARIABLES # # #-------------#
# # # # # VARIABLES #
# # NAME = bin_output # # #-------------#
# # NAME = program_test
# # CC = gcc # # CC = gcc
# # CFLAGS = -I. -c # # CFLAGS = -I.
# # LFLAGS = -L. -ltest
# # DEPS = header.h # # DEPS = header.h
# # SRCS = main.c \ # # SRCS = main.c \
# # function01.c \ # # function01.c \
# # function02.c # # function02.c
# # ODIR = ./builds # # ODIR = ./builds
# # OBJS = $(addprefix $(ODIR)/, $(SRCS:.c=.o)) # # OBJS = $(SRCS:%.c=%.o)
# #
# #
# # # RULES TO EXECUTE
# # # #
# # #---------#
# # # RULES #
# # #---------#
# # all: $(ODIR) $(NAME) # # all: $(ODIR) $(NAME)
# # $(NAME): $(OBJ) # # $(NAME): $(OBJS) $(DEPS)
# # ar -rc $@ $< # # $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
# # #clean: # # %.o: %.c
# # $(CC) $(CFLAGS) -c -o $@ $<
# # clean:
# # /bin/rm -rf $(ODIR) # # /bin/rm -rf $(ODIR)
# # fclean: clean # # fclean: clean
# # /bin/rm -f $(NAME) # # /bin/rm -f $(NAME)
# # re: fclean all # # re: fclean all
# # .PHONY: all clean fclean re # # .PHONY: all clean fclean re
# #
# apres avoir ecrit "make" dans le terminal le Makefile # # prompt "make" execute the first rule ("all")
# va s'executer et l'architecture du fichier racine sera
# un peu changee :
# #
# [archtecture] # [archtecture]
# # main.c # # main.c
# # main.o # # main.o
# # function01.c # # function01.c
# # function01.o # # function01.o ++
# # function02.c # # function02.c
# # function02.o # # function02.o ++
# # header.h # # header.h
# # libtest.a
# # Makefile # # Makefile
# # bin_output # # program_test ++
# #
# -----
# #
# shape of a rule :
# #
# # - "addprefix" is a built-in expansion function used by # target: prerequisites ...
# # makefile to perform a transformation on file names, in # recipe ...
# # this case we want to put all the .o files in a #
# # subdirectory named "build" # when a target is called, its execution depends of the
# # - $(SRCS:.c=.o) is a "substitute reference", an # prerequisites, if they have been more recently modify
# # abbreviation for the expansion function "patsubst" : # than the target the makefile execute the recipe
# # $(patsubst %.c,%.o,$(SRCS)) #
# OBJ = $(addprefix $(ODIR)/, $(SRCS:.c=.o)) # to execute a rull you have to call it's name with make :
# "make rule", and by default the first rule is execute
#
# -----
#
# "automatic variables"
#
# $@ is the name of the target that called the rule
# $< is the name of the first prerequisite
# $^ is the name of all the prerequisites
#
# -----
#
# $(SRCS:%.c=%.o)
#
# is a built-in function called a "substitute reference",
# an abbreviation for the expansion function "patsubst" :
#
# $(patsubst pattern,replacement,text)
# $(text:pattern=replacement)
#
# $() is a variable which expand in its value
#
# % match everything, the value of the first % in
# "replacement" is replaced with the text matched by the
# first % in "pattern", it only works for the firsts
#
# -----
# #
# # - when you write "make" in command line it will execute
# # the first rule wrote in the Makefile, wich is "all" by
# # convention because usually when you type "make" you
# # expect it to build all
# # - it first verify if ODIR is created and if not create
# # it, then it calls NAME to create the library
# all: $(ODIR) $(NAME) # all: $(ODIR) $(NAME)
# #
# ALL depends on ODIR and NAME, so if ODIR doesn't exist
# it will be created, and then NAME is called
#
# -----
#
# $(NAME): $(OBJS) $(DEPS)
# $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
#
# NAME depends on OBJS and DEPS, so if any .o or any .h
# have more recent date of modification NAME will execute
# only for them
#
# -----
#
# %.o: %.c
# $(CC) $(CFLAGS) -c -o $@ $<
#
# when $(OBJS) expand in the list of .o files at the call
# of the NAME rule, each of them call the rule "%.o" wich
# depends on the equivalent .c, so if one has been
# modified since last execution of ALL, it's recompiled in
# object file with use of automatic variables $@ and $<
#
# -----
#
# #clean:
# /bin/rm -rf $(ODIR)
#
# "make clean" suppress all the .o files
#
# -----
#
# fclean: clean
# /bin/rm -f $(NAME)
#
# "make fclean" call CLEAN and suppress the executable
#
# -----
#
# re: fclean all
#
# "make fclean" basically rerun make
#
# -----
#
# .PHONY: all clean fclean re
#
# a phony target is one that is not the name of a file
# if a file called "clean" for instance exist, the rule
# CLEAN will never be executed when prompt "make clean"
# because it will be considered up to date, but if it's
# declared to be phony it will be executed in any case
# - - - - - - - - - - - - - - -
# makefile with subdirectories
# - - - - - - - - - - - - - - -
# put the .o in a "builds/" directory
#
# have the .c in a "srcs/" directory
#
# have the .h in an "includes" directory
#
# have multiples .a files
#
# [archtecture]
# # srcs/
# # main.c
# # function01.c
# # function02.c
# # includes/
# # header01.h
# # header02.h
# # libtest.a
# # liboption.a
# # Makefile
#
# # #-------------#
# # # VARIABLES #
# # #-------------#
# # NAME = program_test
# # CC = gcc
# # VPATH = srcs
# #
# # IDIR = includes
# # _DEPS = header01.h \
# # header02.h
# # DEPS = $(_DEPS:%.h=$(IDIR)/%.h)
# #
# # LDIR = ./
# # _LIBS = libtest.a \
# # liboption.a
# # LIBS = $(_LIBS:lib%.a=%)
# #
# # SRCS = main.c \
# # function01.c
# # function02.c
# # ODIR = ./builds
# # OBJS = $(SRCS:%.c=$(ODIR)/%.o)
# #
# # CFLAGS = -I$(IDIR)
# # LFLAGS = -L$(LDIR) -l$(LIBS)
# #
# # #---------#
# # # RULES #
# # #---------#
# # all: $(ODIR) $(NAME)
# # $(NAME): $(OBJS) $(DEPS)
# # $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
# # $(ODIR):
# # mkdir -p $@
# # $(ODIR)/%.o: %.c
# # $(CC) $(CFLAGS) -c -o $@ $<
# # clean:
# # /bin/rm -rf $(ODIR)
# # fclean: clean
# # /bin/rm -f $(NAME)
# # re: fclean all
# # .PHONY: all clean fclean re
#
# # prompt "make" execute the first rule ("all")
#
# [archtecture]
# # srcs/
# # main.c
# # function01.c
# # function02.c
# # includes/
# # header01.h
# # header02.h
# # builds/
# # main.o
# # function01.o
# # function02.o
# # libtest.a
# # liboption.a
# # Makefile
# # program_test
#
# -----
#
# LIBS = $(_LIBS:lib%.a=%)
#
# from "libtest.a" abd "liboption.a" it creates "test"
# and option
#
# as said, the value of the % in "replacement" is
# replaced with the text matched by the % in "pattern"
#
# -----
#
# OBJS = $(SRCS:%.c=$(ODIR)/%.o)
#
# creates the list of .o from the .c with addition of the
# path directory "builds/main.o"
#
# -----
#
# all: $(ODIR) $(NAME)
# $(ODIR):
# mkdir -p $@
#
# ALL call the rull ODIR wich verify if the objects files
# directory already exist and if not, create it
#
# -----
#
# $(NAME): $(OBJS) $(DEPS)
# $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
#
# NAME depends on OBJS and DEPS, so if any .o or any .h
# have more recent date of modification NAME will execute
# only for them
#
# -----
#
# clean:
# /bin/rm -rf $(ODIR)
#
# "make clean" suppress all the .o files
#
# -----
#
#
#
#
#
#
#
#
#
#
#
# - - - - - - - - - - - - - - - - - - -
# makefile qui make un autre makefile
# - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - -
# makefile pour une librairie
# - - - - - - - - - - - - - - -
# # create the folder where all the .o files will be stored # # create the folder where all the .o files will be stored
# $(ODIR): # $(ODIR):
# mkdir -p $(ODIR) # mkdir -p $(ODIR)

View File

@@ -13,7 +13,7 @@ LDIR = ./
_LIBS = libtest.a _LIBS = libtest.a
LIBS = $(_LIBS:lib%.a=%) LIBS = $(_LIBS:lib%.a=%)
# FUNCTIONS et OBJETS # # FUNCTIONS et OBJETS #
SRCS = main.c #putchar.c transform.c SRCS = main.c to_uppercase.c #putchar.c transform.c
ODIR = ./builds ODIR = ./builds
OBJS = $(SRCS:%.c=$(ODIR)/%.o) OBJS = $(SRCS:%.c=$(ODIR)/%.o)
# OPTIONS de GCC # # OPTIONS de GCC #
@@ -25,7 +25,7 @@ LFLAGS = -L$(LDIR) -l$(LIBS)
#---------# #---------#
all: $(ODIR) $(NAME) all: $(ODIR) $(NAME)
$(NAME): $(OBJS) $(DEPS) $(NAME): $(OBJS) $(DEPS)
$(CC) $(CFLAGS) -o test $(OBJS) $(LFLAGS) $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
$(ODIR): $(ODIR):
mkdir -p $@ mkdir -p $@
$(ODIR)/%.o: %.c $(ODIR)/%.o: %.c

Binary file not shown.

BIN
test/builds/to_uppercase.o Normal file

Binary file not shown.

View File

@@ -5,5 +5,6 @@
void ft_putchar(char c); void ft_putchar(char c);
int transform(int a); int transform(int a);
int to_uppercase(int a);
# endif # endif

View File

@@ -3,7 +3,8 @@
int main() int main()
{ {
int a = 't'; int a = 't';
a = to_uppercase(a);
a = transform(a); a = transform(a);
ft_putchar(a); ft_putchar(a);
return (0); return(0);
} }

7
test/srcs/to_uppercase.c Normal file
View File

@@ -0,0 +1,7 @@
#include "test.h"
int to_uppercase(int a)
{
a -= 32;
return(a);
}

BIN
test/test

Binary file not shown.