My implementation of some of the Standard C Library functions including some additional ones.
My implementation of some of the Standard C Library functions including some additional ones.
Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.
Disclaimer: Reinventing the wheel is bad, 42 makes us do this just so we can have a deeper understanding of data structures and basic algorithms. At 42 we’re not allowed to use some standard libraries on our projects, so we have to keep growing this library with our own functions as we go farther in the program.
As you can see from the Project instructions, there are 4 sections:
Libc functions | Additional functions | Bonus Functions | Personal Functions |
---|---|---|---|
memset | ft_memalloc | ft_lstnew | ft_capitalize |
bzero | ft_memdel | ft_lstdelone | ft_countwords |
memcpy | ft_strnew | ft_lstdel | ft_islower |
memccpy | ft_strdel | ft_lstadd | ft_isupper |
memmove | ft_strclr | ft_lstiter | ft_strndup |
memchr | ft_striter | ft_lstmap | ft_lst_reverse |
memcmp | ft_striteri | ft_realloc | |
strlen | ft_strmap | ft_strjoinch | |
strdup | ft_strmapi | ft_strnchr | |
strcpy | ft_strequ | ft_copyuntil | |
strncpy | ft_strnequ | ft_strstartswith | |
strcat | ft_strsub | ft_intlen | |
strlcat | ft_strjoin | ft_strendswith | |
strchr | ft_strtrim | ft_pathjoin | |
strrchr | ft_strsplit | ft_lstaddback | |
strstr | ft_itoa | get_next_line | |
strnstr | ft_putchar | ft_putnstr | |
strcmp | ft_putstr | ft_strreplace | |
strncmp | ft_putendl | ft_isemptystr | |
atoi | ft_putnbr | ft_strsplitall | |
isalpha | ft_putchar_fd | ft_countwordsall | |
isdigit | ft_putstr_fd | ft_freestrarr | |
isalnum | ft_putendl_fd | ft_strjoincl | |
isascii | ft_putnbr_fd | ft_strjoinchcl | |
isprint | ft_count2darray | ||
toupper | ft_strarrmax | ||
tolower | ft_get_parent_path |
Notes:
My code is not the best, but it passed all the 42 tests successfully.
The goal is to create a library called libft.a from the source files so I can later use that library from other projects at 42.
To create that library, after downloading/cloning this project, cd into the project, copy all the files from the sub folders to the root directory and finally, call make:
git clone https://github.com/R4meau/libft
cd libft
make copy
make
You should see a libft.a file and some object files (.o).
Now to clean up (removing the .o files and the .c files from the root), call make clean
WARNING: make clean
will delete all your files from your root directory. Do not run it if you’re using the Makefile
file. This is why I added the Makefile-sample
file.
I added an example file called example.c, it’s using the function ft_putstr to print “DON’T PANIC” to the screen.
If you try to compile it with gcc using gcc example.c
you will get an undefined symbol error for ft_putstr.
You have to tell the file where your library resides and which library it is using:
gcc example.c -L. -lft
-L takes the path to your library. .
in this case
-l takes the name of your library. This is the set of characters that come after lib
in your library name.
That’s it. Now run it using ./a.out
To test the code we’re going to be using @alelievr’s Libft Unit Test. There are some good others but I’ll only be covering this one.
Clone this repo and cd into it, make sure it’s called libft
:
git clone https://github.com/R4meau/libft
cd libft/
Copy all the source files to the root directory:
make copy
Run Make so you can build the library:
make
Go back to the root directory and download @alelievr’s Libft Unit Test:
cd ..
git clone https://github.com/alelievr/libft-unit-test
Go into the test folder and run the test:
cd libft-unit-test/
make f
If you did everything correctly you should get a cool list of tests showing you the function names and if they passed or not.
You might want to have a go at this project too. If you’ve never heard of Makefiles, don’t worry, you don’t have to learn about it now. So go ahead and follow those steps:
Create a directory for your project, make sure you call it libft
:
mkdir libft
Clone this repo (don’t name it libft) and copy the Makefile-sample as Makefile and libft.h to your own project:
git clone https://github.com/R4meau/libft r4-libft
cp r4-libft/Makefile-sample libft/Makefile
cp r4-libft/libft.h libft/
Go to your project, read the instructions for the function you want to create, code it and uncomment it from the Makefile:
cd libft
vim ft_memset.c
vim Makefile
As an example, after creating ft_memset as your first function, you go into the Makefile, remove the #
in front of FILES
, remove the \
at the end of ft_memset
and add a #
in front of ft_bzero
.
If it still looks complicated, DON’T PANIC, just ask me 😃
Run Make so you can build the library:
make
Go back to the root directory and download @alelievr’s Libft Unit Test:
cd ..
git clone https://github.com/alelievr/libft-unit-test
Go into the test folder and run the test:
cd libft-unit-test/
make f
That’s it! If you’re having some problems, just send me a tweet. If you think your problem is due to my code or this README, create a new issue. I’ll definitely check it out.
This is a list of my projects that use Libft extensively:
Enjoy.