banner



How To Make A Command

Linux brand control

The Linux make command is used to build and maintain groups of programs and files from the source code. In Linux, it is ane of the most frequently used commands by the developers. It assists developers to install and compile many utilities from the terminal. Further, information technology handles the compilation procedure of the sizeable projects. Information technology saves the compilation fourth dimension.

The main motive of the make command is to determine a large program into parts and to bank check whether it needs to be recompiled or not. Besides, it issues the necessary orders to recompile them.

In this section, nosotros will utilise C++ programs since the C++ programming language is an object-oriented language, but you can employ any linguistic communication installed on your machine. Information technology is not but limited to programs; we can employ it to describe other tasks also.

How brand command works?

The make control takes targets as arguments. These arguments are specified in 'Makefile.' The makefile contains the targets likewise every bit associated actions related to these targets.

When nosotros execute the make control, it searches for the makefile and scans it to detect the target and access its dependencies. If dependencies are non specified, it will search for the dependency and will build it. It will build the main target after the dependencies are built.

For example, if nosotros want to modify just 1 source file and nosotros execute the make command; so, this will compile only the object file that is connected with that source file. It will save a lot of fourth dimension in the terminal compilation of the project.

What is Makefile?

The make control invokes the execution of the makefile. It is a special file that contains the shell commands that we create to maintain the projection. The makefile contains targets and commands for execution. It is not allowed to create more ane makefile. Information technology is recommended to create a split up directory for it.

It keeps track of recent files, then only update those files which are required. If we accept a large program with many source files, we must recompile all the dependent files. So, it may be an extremely time taking process.

The makefile has a list of standards. These standards are helpful for the organisation to understand what command nosotros want to execute. These standards are in two parts and separated past a new line. The first line is the dependency line, and the subsequent lines are considered equally actions or commands. The commands are separated with a tab in the new line.

The dependencies specify the relation of each file with the source files. And the target is an executable file, and it is created afterward the execution of make command.

Options

The brand command facilitates various options to brand information technology more specific. Some important options are as follows:

  • -b, -m: These options are used to ignore the compatibility for the different versions of the brand command.
  • -B, --always-brand: These options are used to unconditionally brand all targets.
  • -C dir, --directory=dir: These options are used to modify the directory before executing the makefile.
  • -d: Information technology is used to print the debugging information.
  • --debug[=FLAGS]: Information technology is used to print the debugging information along with normal processing. If we skip the flag, then information technology volition evidence similar results as the '-d' option.
  • -due east, --environment-overrides: It is used to provide the variables taken from the environment precedence to makefile.
  • -f file, --file=file, --makefile=FILE: It is used to use a file as a makefile.
  • -i, --ignore-errors: The '-i' selection is used to ignore all errors in commands.
  • -I dir, --include-dir=dir: It is used to specify a directory to search for the specified makefile. If we specify the many '-I' options, it will search in the many directories in the order are specified.
  • -j [jobs], --jobs[=jobs]: Information technology is used to specify the number of jobs to run simultaneously. If we provide many '-j' pick, the last ane will be considered to execute. If we practice not specify the number of jobs, it volition not limit the jobs that can run simultaneously.
  • -k, --keep-going: Information technology is used to continue the program as much equally possible after getting an error.
  • -50 [load], --load-average[=load]: It is used to specify that no new job should be started if other tasks are in the queue and the load average is at its minimum.
  • -n, --only-print, --dry-run, --recon: It is used to display the command that would be run.
  • -o file, --sometime-file=file, --presume-old=file: It is used to assure that the make will not remake the file even if it is older than its dependencies.
  • -O[type], --output-sync[=blazon]: It is used to ostend the output of each task is put together rather than an inter-mixed output of other tasks. It is useful for multiple task processing with the '-j' selection.
  • -p, --impress-data-base: Information technology is used to impress the database that produces after reading the makefiles. It is also useful to print the version information when used with the '-5' option. To print the database without trying to remake any files, execute the command as follows:

    make -p -f/dev/null.

  • -q, --question: The '-q' option is used for the Question mode. It will not run whatever command or impress anything. It volition only return an leave status is cipher if the specified target is already synced; otherwise, information technology volition brandish a non-nothing get out status.
  • -r, --no-builtin-rules: It is used to eliminate the use of the built-in implicit rules.
  • -R, --no-builtin-variables: It is useful if we do not desire to define any built-in variable.
  • -s, --silent, --quiet: These options are called Silent functioning. It restricts to impress the commands as they are executed.
  • -South, --no-proceed-going, --finish: Information technology is used to abolish the effect of "-k, --go along-going" operation.
  • -t, --touch: Information technology is used to touch files instead of running their commands.
  • --trace: Information technology is used to trace the disposition of each target.
  • -v, --version: It is used to impress the installed version of the make utility. Further, it displays a list of authors, copyright, and some notice regarding the make utility.
  • -westward, --print-directory: It is used to track a impress message that contains a working directory before and later other processing. It is useful to track downwardly errors from the complicated structure of recursive make command.
  • --no-print-directory: It is used to turn off the '-w' choice.
  • -Due west file, --what-if=file, --new-file=file, --assume-new=file: These options pretend that the target file has only been modified.
  • --warn-undefined-variables: This option is used to warn that an undefined variable is referenced.

Let's sympathise some examples of the make command. We will run across the basic utilize of makefile, and farther, nosotros will create some c++ programs and a makefile. We will perform some operations on them to better understand the make command.

Basic use of make command

Permit's understand the very basic apply of brand control, and information technology may assist you to understand how it works.

Create a directory 'project' and change directory to it. Consider the beneath commands:

Now create a "Makefile" having the following content for the first plan:

From the higher up file, the say_hello is a target that behaves like a office in whatever programming linguistic communication, and echo will exist considered equally an activeness. It must be remembered that activity should be written by using a TAB. The target and action together create a dominion for the makefile. Now, execute the brand command every bit follows:

Consider the below output:

Linux make command

From the above output, we can run into the echo operation itself is displaying. If we don't want to display the repeat command on the output, execute the echo starting with '@' symbol. To suppress the echo, update the content of makefile as follows:

Consider the below output:

Linux make command

A target might be a binary file that depends on the actions.

Permit'due south add a few more than targets, such equally generate and list in the makefile. Update the makefile as follows:

If nosotros execute the make command, it only executes the offset target because it is the default target of the makefile. Consider the beneath output:

Linux make command

We can change the default target by including beneath content to our makefile:

Add it to the first line of the file as follows:

Linux make command

The above makefile will consider 'generate' as the default goal. Execute the make command, and it will give the output as follows:

Linux make command

The DEFAULT GOAL selection will execute but 1 target to specify more than than one target to use all options. To specify more one target, update the first line of the makefile as follows:

It volition execute the specified target. Consider the below output:

Linux make command

There is another option that allows united states of america to execute all the targets. If we want to execute all the targets of the makefile, update the file as follows:

The above file volition perform all specified targets. Execute the make command, consider the below output:

Linux make command

Avant-garde use of the make command

Allow'southward create a C++ project having files main.cpp, function1.cpp, function2.cpp and a dependency file function.h.

The code of the files is as post-obit:

main.cpp:

function1.cpp:

function2.cpp:

functions.h:

Now create an executable file of the above project by executing the beneath command:

The above command volition create an executable file 'hello' of the files main.cpp, function1.cpp and function2.cpp.

Consider the below output:

Linux make command

From the higher up output, if information technology is successfully executed, it volition non give any output.

Let'southward perform the same task past using the makefile.

Create a file every bit Makefile and put the beneath code in it.

The all keyword is used for target and in newline put the same command with a TAB every bit to a higher place to specify the operation. Salve the file. Consider the below file:

Linux make command

To operate, execute the control as follows:

The higher up command will create an executable file 'hi' of the specified files. Consider the below output:

Linux make command

Permit'due south add some more tasks to Makefile. Add a task 'compile' as follows:

To execute the task compile, execute the below command:

The above control will execute the compile task. Consider the below output:

Linux make command

Allow'due south perform some more tasks to our makefile.

update the Makefile as follows:

From the to a higher place makefile, we have created three objects as main.o, function1.o, and function2.o. Farther, we have provided the dependencies for the targets main.o, function1.o and function2.o as main.cpp, function1.cpp and function2.cpp respectively. All the targets will perform the specified tasks inside information technology. We accept also specified a clean target to clean all the dependencies and remove the executable file.

At present execute the make all command to execute our new makefile.

Consider the below output:

Linux make command

From the above output, nosotros can come across that the command first executed the primary.o, function1.o and function2.o respectively. It will create the executable and objects file of the given files. It volition non execute the clean target because we have non specified it in hello. Consider the below files:

Linux make command

The make command has a straight frontwards working process. It executed the all pick and went to hello. After the execution of hello, it read the target in the specified sequence. Information technology searched every target and its dependency and executed them in the sequence.

To remove the objects and executable file, perform the make clean task. To perform the clean task, execute the control equally follows:

Consider the below output:

Linux make command

The above control will remove all the objects and executable files. See the beneath snap of the directory:

Linux make command

From the higher up image, nosotros tin can see that nosotros have cleaned our directory.

Variables in make command

Nosotros can ascertain the variables in our makefile. To define the variables, utilize the '=" operator. For case, if we want to create a variable A and assign a control gcc to it, assign it as:

Use it as follows in our makefile:

It is passed to the terminal as:

Nosotros can use $(A) instead of ${A}, as both are treated the aforementioned by the script.

Comments in makefile

To add a comment into the makefile, use the '#' symbol. For case, to add together a annotate in the compile section, add together it as "# This volition compile the program." Comments are ignored past the compiler.


Source: https://www.javatpoint.com/linux-make-command

Posted by: vasqueztherne.blogspot.com

0 Response to "How To Make A Command"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel