If your project has many files, translating and linking them will take time. To solve this problem, you can save your commands to translate your program under a specific format in a text file named Make File without extension next to the project. This article will introduce you to 5 Ways to Automate Your Linux Builds with Make Files. Check out the packages offered on the Eldernode website to purchase your own Buy Linux VPS Hosting.
Table of Contents
Automating Linux Builds with Make Files
Make is a Unix utility used to run or update a task when updating specific files and requires a Make File to define a set of tasks to run. Make File is a set of commands necessary to compile a software project, stored in a special format in a text file. If you type make in the make file directory, the commands in the make file will be executed. The Make File also contains a list of rules which will tell the system what commands you want to be executed. Here are 5 ways to automate your Linux Builds with Make Files.
In the Continuation of this article from the Linux training series, you will learn how to automate your Linux Builds with Make files in 5 ways.
1- Install GNU Make
GNU Make is a tool that gets its knowledge to build a program from a make file. Make File lists for each of the non-source files and how to calculate them from the other files. controls the generation of a program’s executable and other non-source files from the source files program. In fact, GNU Make contains the generation of executable files and other non-source files of a program from the program’s source files.
In Linux operating systems, the make program may be already installed. If it doesn’t install, follow the below instructions to install it.
If you are using Ubuntu/Debian, enter the following command to install the GNU Make:
apt install make
You can get the most out of make by installing the development tools which come with your distribution:
apt install build-essential
To install GNU Make on Fedora/RHEL, run the following command:
yum install make
If you want to get the most out of make, install the development tools as shown below:
yum groupinstall ‘Development Tools’
Install GNU Make on Arch/Manjaro, use the following command:
pacman -S make
Enter the following command to install development tools:
pacman -S base-devel
2- Create a new directory for your project
First of all to create a new directory for your project, run the following command:
Now check if the directory is created or not with the command below:
3- Add a file called Makefile
In the first step, you should navigate to the created directory to add the Makefile file. To do this, run the following command:
Once you navigated to the directory, you can add Makefile to it with the following command:
You can also use text editors to add Makefile to the directory as shown below:
You can check whether a file is added or not with the command below:
4- Write some rules
Now it’s time to write some rules for Makefile. If you want to do this, the general syntax of the Makefile rule is as below:
target [target...] : [dependent ....] [ command ...]
You can write desired arguments in brackets and there are optional. Note that ellipsis means one or more and the tab is needed to preface each command.
For example, if you want to define a rule to make your target hello from other files, write rules as follows:
hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello
If a command returns a failure status, you should make terminates as shown below:
clean: -rm *.o *~ core paper
The returned status on command lines that starts with a dash is ignored by Make.
Everyone expects certain targets in Makefiles. You can expect that all targets are found make, install and clean:
1- make all: Compiles everything to do local testing before installing applications.
2- make install: Installs applications in the right places.
3- make clean: Cleans applications, get rid of the executables, any temporary files, object files, etc.
5- Run make
The make utility works when a makefile is read and interpreted. To run make, enter the following command:
Now GNU make will look for a file named Makefile, makefile, or GNUmakefile. Once one of these makefiles is founded, the first target will be built.
If make cannot find an appropriate makefile, you will see the following error:
make: *** No targets specified and no makefile found. Stop.
You can use the -f option to specify the makefile. The make command syntax is as follows:
make -f <i>filename</i>
Remember to replace the name of your makefile instead of the filename.
If you want to build a specific target out of several targets defined in the makefile, use the syntax below while running make:
Make Files defines a set of tasks to be executed and you can use it to compile a program from source code. In this article, we introduced you to 5 ways to automate your Linux Builds with Make Files. If you face any problems or have suggestions, you can contact us in the Comments section. I hope you found this tutorial useful and helps you to automate your Linux Builds.