Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

Learning about the make command in Linux

Last Modified : 04 Mar, 2023 / Created : 04 Mar, 2023
371
View Count
The make command is commonly used in the Linux environment and is easily recognizable. However, let's take a closer look at what make is and what functions and roles it performs.




# Understanding Linux make


The make command is especially useful in large and complex projects, rather than smaller ones. Make allows you to declare and group various tasks to make them easy and convenient to execute. This is why it is commonly used for compiling and transforming in CI/CD environments for building and deployment purposes.

Now let's take a closer look at how to use make and its syntax below.


! Learn how to use make


Briefly to use make you first need to understand Makefile. Makefile is a list that defines the tasks that make will perform and is located at the root of the project.

* Makefile
  • You can declare or use block-level commands saved in a file located in the root of the project path with the file name "make".

The most essential requirement for executing make is like this. If there are no files in the page root? In this case, you can create an empty file and start right away. After adding the command to show 'Webisfree.com', the following Makefile was executed using make. First, create a Makefile.
> vim Makefile

# Print Webisfree


showSitename:
    echo 'Webisfree.com'

As mentioned above, # can be used as a comment within a Makefile. After specifying a block like showSitename, use a colon to separate it. The commands to be executed are laid out below and must be separated using the Tab key. Although only one line was used above, multiple commands can be used by using line breaks.

Now let's try running it using make.. (By the way, make only performs the top code block!)
> make

echo 'Webisfree.com'
Webisfree.com

When executing make, the declared command is called. In the example above, we used one showSitename: command within the file and wrote the code to output 'Websifree.com' below it. When we then execute make, the first line shows the executed command and the following line displays its result.

This was the simplest way to call a command. Now let's look at the case of using two blocks. It is possible to use multiple blocks, even if there are more than two.
showSitename:
  echo 'Webisfree.com'

showProtocol:
  echo "https"

If you only want to execute 'https' in the above, you can use make <blockName> like the example below.
> make showProtocol

echo "https"
https

It is possible to select only what you want and execute it like this. Note that the part that prints the command may not be necessary to show. In other words, in the above execution result, if 'https' in echo is unnecessary, it will not be displayed. In this case, you can use the @ symbol. Let's modify it as follows.
showProtocol:
  @echo "https"

What will happen if we run the code now? Only the "https" will be outputted, excluding the "echo 'https'."
echo "https"

In addition, if you want to run both blocks, you can use space in "make" to use each block name separately. Please see the code below.
> make showSitename showProtocol

echo 'Webisfree.com'
Webisfree.com
https

By the way, if you change the order, it will be executed in the changed order. Although it is possible to declare all block names in this way, it is cumbersome, so a simpler method is needed. In this case, you can write code to execute other block names within the code.


@ Passing variables in Makefile
You can pass the value of the desired variable when running "make" additionally. This is very useful when you need to pass changing environment variables or other variables in certain situations. For example, when passing the current deployment environment, you can use it as follows.
> make env=dev

@echo The current execution environment is ${env}.

If executed, it will be printed as follows.
The current execution environment is dev.


@ Executing different blocks within Makefile.
The method is simple. You just need to list the names of the blocks to execute after the colon of the block. You can also use multiple by using spaces! Here, a new block name, "all:", is declared and written to execute the entire code below.
all: showSitename showProtocol

showSitename:
  echo 'Webisfree.com'

showProtocol:
  @echo "https"

Now, if you run 'make' or 'make all', all the pre-declared blocks will be executed.

By the way, we used the # symbol to use variables above. Additionally, it is possible to declare variables using the "=" sign. The declared variables can be used like ${variable}! Here's a simple example.


@ Declaring and using variables in Makefile
"Let's declare a sitename variable and use it by calling it simply."
sitename='Webisfree.com'

showSitename:
  echo ${sitename}

If you run it, it will output the previously declared variable using echo. It works well! Note that even if the variable is declared afterwards, it will still be hoisted and output without errors, so please keep that in mind.


So far, we have learned a brief way to use make in Linux together.
Perhaps you're looking for the following text as well?

    Previous

    How to automatically start services when restarting a Linux server

    Previous

    Learn about the history command in Linux