HOME / BLOG / MLX Starter Pack
minilibx made easy for you!
In this tutorial, I will show you the best way to start a project that uses minilibx.
First and more importantly I encourage you to read the 5 manuals RTFM.
First and more importantly I encourage you to read the 5 manuals RTFM.
I personally don't think that you should understandEVERYTHINGthat is in the manual, you only have to read them and know the information inside the manual.
This is extremely helpful because when you have a question/problem you know where to find the answer, and at that point, you will need to understand that specific part of the manual.
- mlx
- mlx_loop
- mlx_new_image
- mlx_new_window
- mlx_pixel_put
Covering The Basics
"You must use the MiniLibX. Either the version available on the school machines or installing it using its sources." (from the subjects)
This means that your project should compile and work with the school's machines and also with the sources that they give you on the project page.
(The sources are provided on the project page as shown below).
Probably you want to be able to run your code on your personal computer, to do so follow these steps.
Go to your project page at intra.42.fr and download the adequate source for your OS...
If you rather feel "powerful" you can execute something like this:
Go to your project page at intra.42.fr and download the adequate source for your OS...
If you rather feel "powerful" you can execute something like this:
cd ~/ mkdir tutorial_minilibx cd tutorial_minilibx curl -O https://projects.intra.42.fr/uploads/document/document/8330/minilibx_opengl.tgz tar -xvf minilibx_opengl.tgz ls minilibx_opengl_20191021/
Headers and Libraries
Headers
All computers should have a "usr/local/include" folder (or equivalent) where the user can create header files that will be included automatically when compiling your code.
At this point, you should have noticed that when you include standard libraries you use <lib.h>, and when you want to include your own headers you use "mylib.h".
I think it's logical to add minilibx headers to this folder so that your project can compile using #include <mlx.h>.
https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html - https://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html (You can read more about how Include works here)
At this point, you should have noticed that when you include standard libraries you use <lib.h>, and when you want to include your own headers you use "mylib.h".
I think it's logical to add minilibx headers to this folder so that your project can compile using #include <mlx.h>.
https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html - https://gcc.gnu.org/onlinedocs/cpp/Environment-Variables.html (You can read more about how Include works here)
If you are curious you can find out all the paths where gcc looks for your headers, for ".h" & <.h> ...
Run this to find out :
Run this to find out :
echo | cpp -Wp,-v
If you are even more curious to understand what that command was, you can find out with: gcc -help or man cc
- cpp (or gcc -E) (https://gcc.gnu.org/onlinedocs/cpp/Invocation.html)
- -E Only run the preprocessor.
- -Wp,<arg> Pass the comma separated arguments in <arg> to the preprocessor.
- -v Show commands to run and use verbose output.
After you founded out where gcc looks for your headers you can copy the 5 headers that you downloaded from minilibx source:
- mlx.h
- mlx_int.h
- mlx_new_window.h
- mlx_opengl.h
- mlx_png.h
Now, you will be able to include the headers with <mlx.h> and you won't have to worry about linking them, and yes the school machines have these headers installed.
MLX library
As you might remember from libft, libraries help you collect/arrange a set of functions, definitions, structures; etc all in one place and you only need to compile them once.
You will need to compile the mlx source files that you downloaded. If you were meticulous you would have noticed that the sources came with a Makefile.
Compile the minilibx source Makefile with "make".
After 100000 warnings you should get your precious libmlx.a (i.e. your OS minilibx library!)
Sometimes there is a test folder in the minilibx project, don't hesitate to run the executable and try to understand what's happening!
You will need to compile the mlx source files that you downloaded. If you were meticulous you would have noticed that the sources came with a Makefile.
Compile the minilibx source Makefile with "make".
After 100000 warnings you should get your precious libmlx.a (i.e. your OS minilibx library!)
Sometimes there is a test folder in the minilibx project, don't hesitate to run the executable and try to understand what's happening!
Libraries
Just like the Headers, you can also add local libraries to your computer. You can also use this tutorial to add your libft.a to your computer :D
gcc will also look for your local libraries, usually under "/usr/local/lib/".
You can just drop libmlx.a and libft.a (if you want) there, and you won't have to worry about linking the libraries.
If you don't want to do this or you don't have permission on the computer to place files there, you can link the libraries, with the "-L PATH_TO_LIBRARY -l lib_name".
Example (assuming libmlx.a is on the path used bellow):
-L tutorial_minilibx/minilibx_opengl_20191021 -l mlx
Now you should be able to include your minilibx headers and library to start your project.
I attached a picture below of a simple main "including" mlx and compiling. You should be able to compile as well...
[Rememeber to copy your libft project if the subject allows it]
I attached a picture below of a simple main "including" mlx and compiling. You should be able to compile as well...
[Rememeber to copy your libft project if the subject allows it]
Makefile
You will need a Makefile to compile your project, this part is very intimate, and every person has their own way to organize their code, therefore compile their code as well.
As a "rule", the Makefile needs to be at the root of your project because it only makes sense. It should have the rules "NAME, all, clean, fclean, re" according to the subjects.
I like to have the Makefile on the root and create folders on the root according to what they are used for. I would create the following folders: "include" "src(sources)" "assets" "maps" "libft" and so forth...
As a "rule", the Makefile needs to be at the root of your project because it only makes sense. It should have the rules "NAME, all, clean, fclean, re" according to the subjects.
I like to have the Makefile on the root and create folders on the root according to what they are used for. I would create the following folders: "include" "src(sources)" "assets" "maps" "libft" and so forth...
Variables
NAME = project_name INC = /usr/local/include# could be /usr/include depending on your OS INCFT = ./libft/include# header from libft LIBFT = ./libft# path to libft library LIBMLX = /usr/local/lib# could be /usr/lib, depends on where you decided to put your mlx library UNAME := $(shell uname)# get the OS name, this will help define behaviors for certain OS's CFLAGS = -Wall -Werror -Wextra -O3 -g -I$(INC) -I${INCFT} -Iinclude# C Flags (gcc) & linking. "-Iinclude" if you created the folder "include" to put your project headers" LFLAGS = -L$(LIBMLX) -lmlx -L${LIBFT} -lft# if you decided to install libmlx.a locally you don't need "-L$(LIBMLX) -lmlx" the school also has it locally as well... SRC = $(wildcard src/*.c)# list your source files OBJ = $(SRC:%.c=%.o)# convert source files to binary list
Enable compilation for Linux, FreeBSD, and darwin.
ifeq ($(UNAME), Darwin) # iMac / iOS CC = gcc LFLAGS += -framework OpenGL -framework AppKit else ifeq ($(UNAME), FreeBSD) # FreeBSD CC = clang else #Linux and others... CC = gcc LFLAGS += -lbsd -lXext -lX11 -lm endif
Compilation.
all: $(NAME) $(NAME): runlibft $(OBJ) $(CC) -o $(NAME) $(OBJ) $(LFLAGS) runlibft: make -C libft --no-print-directory clean: rm -f $(OBJ) fclean: clean rm -f $(NAME) re: fclean all show: @printf "UNAME : $(UNAME)\n" @printf "NAME : $(NAME)\n" @printf "CC : $(CC)\n" @printf "CFLAGS : $(CFLAGS)\n" @printf "LFLAGS : $(LFLAGS)\n" @printf "SRC : $(SRC)\n" @printf "OBJ : $(OBJ)\n"
Tutorial...
If many people like this post I will create a tutorial for a minilibx super small project to get started:
Also you can just download the tutorial or check it out here:
(If you download the tutorial_minilib.zip file you will need to replace the libft with your libft or remove the runlibft rule from the Makefile)
https://github.com/pulgamecanica/42Course/tree/main/42Documentation/tutorial_minilibx
I did create the tutorial... :)
Tutorial
Also you can just download the tutorial or check it out here:
(If you download the tutorial_minilib.zip file you will need to replace the libft with your libft or remove the runlibft rule from the Makefile)
https://github.com/pulgamecanica/42Course/tree/main/42Documentation/tutorial_minilibx
I did create the tutorial... :)
Tutorial