Un petit script pour générer un debut de projet (squelette en C) source.c , le makefile qui va bien
avec la commande make.
skeletonc projet
qui va créer dans le répertoire courant projet.c et un fichier Makefile adapté au projet.
Mais l'idéal est de suivre ce cours pour comprendre comment fonctionne le fichier Makefile en accord avec la commande make de unix/Linux
Dans le fichier Makefile il existe ces variables :
$@: The filename representing the target.
$%: The filename element of an archive member specification.
$<: The filename of the first prerequisite.
$?: The names of all prerequisites that are newer than the target, separated by spaces.
$^: The filenames of all the prerequisites, separated by spaces. This list has duplicate filenames removed since for most uses, such as compiling, copying, etc., duplicates are not wanted.
$+: Similar to $^, this is the names of all the prerequisites separated by spaces, except that $+ includes duplicates. This variable was created for specific situations such as arguments to linkers where duplicate values have meaning.
$*: The stem of the target filename. A stem is typically a filename without its suffix. Its use outside of pattern rules is discouraged.
source : test.c
ou ici test est le nom de l’exécutable et donc le source test.c dans votre projet
#la cible (executable)
TARGET = test
#Makefile petit projet avec skeletonc
CFLAGS = -g -Wall -pedantic -ansi
#compilateur ici gcc
CC = gcc
#section pour avoir TARGET il faut TARGET.c et faire
$(TARGET): $(TARGET).c
$(CC) $< $(CFLAGS) -o $@
#avec edit on aura le source dans vi
edit: $(TARGET).c
vi $(TARGET).c
#on va lancer gdb avec l'executable
gdb: $(TARGET)
$(DEB) $(TARGET)
#on nettoie le projet
clean:
rm $(TARGET)
ici le source c avec le main (donnant l’exécutable)
cptchaine.c
et ses fonctions.. pour réaliser les fichiers objets
malib.c avec son malib.h
/********************************
* Projet : passage de chaine
* Auteur : Bogt
* Date : 28/11/2022
********************************/
#include <stdlib.h>
#include <stdio.h>
#include "malib.h"
int main (int argc, char **argv)
{
int letter_number;
letter_number=lg_chaine ("Bonjour le monde");
printf ("on a %d lettre(s) ou espace(s) dans cette chaine\n",letter_number);
return EXIT_SUCCESS;
}
#include <stdio.h>
/* fonction lg_chaine */
int lg_chaine(char *chaine)
{
int nb_lettre;
nb_lettre=0; /* pas de lettre au départ de la fonction */
printf ("%s \n",chaine);
while (chaine[nb_lettre]!=0) /* 0 ou '\0' */
{
nb_lettre=nb_lettre+1; /* on a une lettre en plus dans la chaine */
}
return nb_lettre; /* retour du nombre de lettre */
}
#ifndef __malib__
#define __malib__
/* fonction lg_chaine */
int lg_chaine(char *chaine);
#endif
cptchaine: cptchaine.o malib.o
gcc -o cptchaine malib.o cptchaine.o -Wall -ansi -pedantic -g
cptchaine.o: cptchaine.c
gcc -c cptchaine.c -Wall -ansi -pedantic
malib.o: malib.c
gcc -Wall -ansi -pedantic -c malib.c
tarball:
tar -zcvf ../cptchaine.tar.gz cptchaine.c malib.c malib.h Makefile
clean:
rm *.o cptchaine
Le tar, ici gz car compressé avec zip
les options -ztvf pour lister l'archive
bruno@elliott:~/Works/langage_C/cptchaine$ tar -ztvf cptchaine.tar.gz
-rw-r--r-- bruno/bruno 403 2023-11-29 16:41 cptchaine.c
-rw-r--r-- bruno/bruno 357 2023-11-29 16:46 malib.c
-rw-r--r-- bruno/bruno 98 2023-11-29 16:45 malib.h
-rw-r--r-- bruno/bruno 323 2023-12-22 15:28 Makefile
prj1 est vide au départ !
pour décompresser -zxvf
bruno@elliott:~/Works/langage_C/cptchaine/prj1$ tar -zxvf cptchaine.tar.gz
cptchaine.c
malib.c
malib.h
Makefile
bruno@elliott:~/Works/langage_C/cptchaine/prj1$ ls
cptchaine.c cptchaine.tar.gz Makefile malib.c malib.h
Makefile plus facile à adapter avec les variables
#la cible
TARGET = cptchaine
RESSOURCES = malib
FLAGS= -Wall -ansi -pedantic -g
CC = gcc
$(TARGET): $(TARGET).o $(RESSOURCES).o
$(CC) -o $(TARGET) $(RESSOURCES).o $(TARGET).o $(FLAGS)
cptchaine.o: cptchaine.c
$(CC) -c $(TARGET).c $(FLAGS)
$(RESSOURCES).o: $(RESSOURCES).c
$(CC) $(FLAGS) -c $(RESSOURCES).c
tarball:
tar -zcvf ../cptchaine.tar.gz cptchaine.c malib.c malib.h Makefile
clean:
rm *.o cptchaine