• Important: We recently upgraded our forum software - please let us know if you run into any issues.

Applications 35 – The “make” Command

J

Jarret W. Buse

Guest
Applications 35 – The “make” Command

Some applications on the Internet are not in a usable form for your Linux distro. The programs are still in source code format. For those who understand a programming language, a program can be modified as needed. Once the source code is as needed, the program language can be compiled into machine language for the computer to understand it. The “make” command is used to compile source code into an adequate format.

The “make” command cannot simply take source code and compile it. What is needed is a file named “makefile”. The “makefile” contains information for “make” to compile and link the source code.
When source code is compiled, it is changed into machine code. The machine code is in an object file (obj). Once all the object files are created, the files are linked into an executable file. The “linker” program creates an executable specific to an architecture. The default linker should be compatible for the system on which it is running.

A main idea to remember is that some source code requires dependencies to be installed on the local system. Others may require a special command to be run first before the source code can be compiled and linked. Check for a “Readme” file to give you the necessary instructions for completing all required processes before running “make”.

The syntax for “make” is: “make [ -f makefile ] [options] target”.

The options are as follows:

-b and -m – These options are ignored for compatibility with other versions of make

-C dir (--directory=dir) – change directory to dir before reading the “makefile”

If the current folder were the HOME folder, but the “makefile” is located in “~/program/”, then you could run the command “make -C program”. The current folder will be changed to “program” before compilation starts.

-d – prints debugging information as well as the normal processing output

To see more “verbose” type output the command would be “make -d”. The output may be quite a bit more than you would expect so use this option cautiously.

-e (--environment-overrides) – uses system variables taken from the environment over variables in the makefiles

If environment variables are set on the system which need to override the variables in the makefile, then use the “-e” option.

-f file (--file=file, --makefile=file) – specifies makefile filename

If you should modify the “makefile” and create another to use, you can specify the new file. A copy of “makefile” can be made and modified called “makefile2”. If you want to compile and link the source code with the new “makefile”, the command would be “make -f makefile2”.

-i (--ignore-errors)– ignore all errors in commands executed to remake files

If errors are generated during a compile then the “-i” option can be used to force the complete to complete. If errors are ignored, and the compile is completed, then you can see all the errors to be able to fix them all at once.

-I dir (--include-dir=dir) – specifies a directory dir to search for included makefiles

If multiple “makefile” are used, then the directories can be specified with the “-I” option. For instance, if a “makefile” is in the current folder and another exists in the “/home/jarret/test/” folder, the command would be “make -f makefile -I /home/jarret/test/”.

-j jobs (--jobs=jobs) – specifies the number of jobs to run simultaneously

The number of compiling processes can be specified by the “-j” option. A general rule is to set the number of jobs equal to the number of cores +1. So, on a quad-core system, the command would be “make -j5”. You can test the number of jobs by timing them and see what works best for your system.

-k (--keep-going) – even if an error occurs, continue processing as long as possible

Continue compiling and linking until a critical error occurs. The errors can help find other errors.

-l load (-load-average=load) – specifies that no new jobs should be started if there are other jobs running and the load average is at least load (a floating-point number). Specifying no argument will remove a previous load limit

The option allows you to set the maximum load average for the system. If the load is over the amount set, then no new jobs can start. When started, “make” will check the current load and if it is over the amount set, it waits until the load drops below the amount to start. To reset the amount set, specify no argument. So to set the load average at 50%, use the command “make -l 50”.

-n (--just-print, --dry-run, --recon) – do not execute commands, but prints the commands that would be executed

The “-n” option prints out all the command executed by “make” without actually executing the commands. The output can show what will be occurring without actually running any commands.

-o file (--old-file=file, --assume-old=file) – do not remake the file even if it is older than its dependencies, and do not remake anything on account of changes in file

The “-o” option can be used so that the “make” program will not remake the compiled and linked program if it is older than dependencies and changes in the file. The output can show you errors in the compiling and linking. For example, to compile “program1” and see the output without updating “program1”, the command would be “make -o program1”.

-p (--print-data-base) – prints the rules and variable values that are from the makefiles

To see the commands being processed by the “make” command without executing any of them, use the command “make -p”.

-q (--question) – “Question mode”, which does not run any commands, or print anything; just returns an exit value that is zero if the specified targets are already up-to-date, nonzero otherwise

To question the compiled program to see if it is up-to-date and get an exit value of zero, use the command “make -q”. Any message given shows the file is not up-to-date.

-r (--no-builtin-rules) – eliminates use of the built-in implicit rules

Causes built-in-rules to be ignored when using “make”.

-s (--silent, --quiet) – does not print the executed commands

The “-s” option does not show the executed commands as they are processed.

-S (--no-keep-going, --stop) – cancels the effect of the -k option

When using the “-S” option, the “-k” option is negated.

-t (--touch) – touch files by marking them up-to-date without making any other changes

Updates any files out of date. Be sure the “makefile” is up-to-date before running.

-v (--version) – prints the version of the make program plus a copyright

To see the version information for “make”, use the “-v” option. All other options are ignored.

-w (--print-directory) – prints a message containing the working directory before and after other processing

To show the directory that “make” is working in before each process starts, use the “-w” option.

-W file (--what-if=file, --new-file=file, --assume-new=file) – pretends that the target file has just been modified. When used with the -n flag, the command shows you what would happen if you were to modify the file. When not using the -n option, it is almost the same as running a touch command on the given file before running make, but no dates are changed

When the option “-W” is used with “-n” the processes are shown, but not executed on the specified files. Without the “-n” option, the “make” command performs a “-t” option on the specified files.
Try the options and see the output to check how things work.
 

Attachments



J

jerz4lunch

Guest
but make needs gcc!
He's just talking about the invocation of the make command, thus he technically doesn't need to mention that you need gcc installed. But you are correct: make is (most of the time) pretty much useless without the GNU Compiler Collection (gcc). So, good point ;)!
 
M

Mitt Green

Guest
He's just talking about the invocation of the make command, thus he technically doesn't need to mention that you need gcc installed. But you are correct: make is (most of the time) pretty much useless without the GNU Compiler Collection (gcc). So, good point ;)!
But gcc is not the only compiler collection out there...
 

Staff online

Members online


Top