Skip to content

gabrielasertori/Libft-42

Repository files navigation

ecole42

42 Cursus - Libft

🚀 Our first library in c!

Sumary

Makefile
Descriptives
Helpfull links

Let's make it right!

So, let's begin with Makefile, huh?

Variables

  1. Declare a variable that will content the name of your library (you'll need it every time when compiling).
NAME: libft.a
  1. Then you may declare variables for the compiler and for the flags.
CC: clang
FLAGS: -Wall -Wextra -Werror
  1. This is important! You need to put all the .c files that you created for the lib. Only this files will be transformed as object files and then compile with the .a
MANDATORY: *.c

or

MANDATORY: ft_isprint.c ft_isalpha.c ft_isnum.c ft_isalnum.c ft_isdigit.c \
				ft_memchr. ft_memcmp.c
  1. Another important step for the mankind! Transform every .c file present in the previous declaration into .o files. Tadaa! You just created object files. a. If there are others .c files that you want to transform and compile separated, than you must declare in another variable and transform in .o separately.
OBJ: $(MANDATORY:.c=.o)
  1. Let's compile with our flags. Every time that we call .c=.o our makefile must compile the .c with the specified flags and output a .o file, like this:
%.o: %.c
	clang -Wall -Werror -Wextra -c $< -o $@

$< refers to every .c file and $@ to .o files that is compiling.

Rules

  1. First rule: you need to create the .a library, without it you can't compile.

  2. How can I create it? Simple, a. You'll need the objects and code with ar command that will create, extract and modify archives for you. b. The options -rc will... r: insert the files and c: create the archive.

ar -rc $(NAME) $(OBJ)
  1. So, the above code will create and add the files *.o into libft.a.

  2. We can make a rule to clean the objects files as well:

clean:
	rm -f $(OBJ).o

Linking

So, okay, we created the library .a with all the .o files that came from .c files. How could we test our functions???

  1. Make a rule for it!
main: main.c $(NAME)
	$(CC) $(FLAGS) main.c $(NAME) -o main
	./main
  1. The above code are compiling the main.c file with your library and output this in an executable named main, and then executing it.

  2. Relink When you do the command make the makefile must compile all your files and turn into your .a library. If you make any change in some files only them had to recompile when you do make, otherwise your code are relinking.


Descriptives

Mandatory part: All .c files have comments about how each function works (except the first six functions)

Bonus part: Bonus also have comments, but I only made until ft_lstadd_back (I'm sorry hehe :D)

Main tests: In this repository you will find a main.c file with functions that I made to tests my library. Feel free to test with your code.

  • You may copy the file and execute with your makefile (that I teach how to do).

  • Or you may copy and paste inside your lib function, rename the function to main(void) and add the return (0) at the end.


Helpfull links

Thanks A LOT to these...

Ctutor Online pictoric debbug that helped me a lot to understand what was going on.

LibftTester Tripouille tests with unimagined parameters and prevents crash, bugs and errors.

Makefiles Video that explains how makefiles works on backstage.

Link to links A notion of notions to libft. Super helpfull.

Memcpy vs memmove This two functions are super similar, but have some differencies.

Malloc Video that explains how malloc function works.

Recursive Functions Video that explains how recursivity works on c (yeh, you'll need this at some point).

Strlcpy It isn't the strlcpy function, but that is interesting to understand why one is safer than the other.

Introduction to list Yep, that graphic explanation was wow!

List part 2 Complementary to the above link, this explanation was incredible and I could understand so much more!


Have tips?

Hey you! Have tips that are helpfull too? Please, feel free to make contact!

Give ⭐ if you like this project, this will help me!

That's it folks! Stay focused and don't panic!

About

The project is the first project of école 42 - sp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages