How to Compile a Program in Linux: 7 Steps

Table of contents:

How to Compile a Program in Linux: 7 Steps
How to Compile a Program in Linux: 7 Steps
Anonim

Source code is the human readable and understandable form of a computer program. However a machine cannot use the source code directly. The code must be compiled, i.e. transformed into machine code before it can be used. On Linux systems, one of the most popular compilation commands is the 'make' command. This command works for compiling almost all of the source code that forms Linux packages.

Steps

Compile a Program in Linux Step 1
Compile a Program in Linux Step 1

Step 1. Download the source code of the program or driver of your interest, from the web or other source

Most likely the file will be in 'tarball' format with the extension '.tar', '.tar.bz2' or '.tar.gz'. However, sometimes an archive in '.zip' format may be used.

Compile a Program in Linux Step 2
Compile a Program in Linux Step 2

Step 2. Unzip the downloaded file

In the case of a '.zip' archive, use the 'unzip [name_fiel]' command. In the case of a '.tgz' or '.tar.gz' file, use the 'tar -zxvf [filename]' command. In the case of a '.bz2' file, use the 'tar -jxvf [filename]' command. Alternatively you can use the graphical interface.

Compile a Program in Linux Step 3
Compile a Program in Linux Step 3

Step 3. Access a terminal window and navigate to the folder where you extracted the downloaded archive

To do this, use the command 'cd [directory_name]'.

Compile a Program in Linux Step 4
Compile a Program in Linux Step 4

Step 4. Run the command '

/ configure 'to automatically configure the source code. Command parameters, such as '--prefix =', can be used to control the installation directory. These types of checks are used to make sure you have the correct libraries and versions.

Compile a Program in Linux Step 5
Compile a Program in Linux Step 5

Step 5. After running the '

/ configure ', execute the' make 'command which will start the compilation (running this command may take a few seconds or several hours). The executable code of the program will be generated in the 'bin' directory located inside the directory where the source code resides.

Compile a Program in Linux Step 6
Compile a Program in Linux Step 6

Step 6. To install the compiled program, use the 'make install' command

Compile a Program in Linux Step 7
Compile a Program in Linux Step 7

Step 7. Finished

You have successfully compiled and installed the source code of your program.

Advice

  • If the build fails for any reason, before trying again, run the 'make clean' command to delete all files related to the previous build. The presence of these files could be the cause of the compilation process failure.
  • On computers that use multicore processors, you can build with multiple processes (multithreaded) using the 'make -j3' command. Replace the number 3 with the number of threads you want to use
  • If the compilation fails you will be given the name of the file that generated the error, the type of error and the number of the line of code where the problem occurs. This way you can try to fix the problem. Most compilation problems are caused by dependencies on the software you are installing - that is, other programs or libraries that it refers to.
  • Unless you specify a different suffix, the code will be automatically installed in the '/ usr' location.
  • You will need to have 'superuser' permissions.
  • You can also chain multiple commands together. For example './configure && make && make install'.

Warnings

  • Compiling and replacing critical system components can cause problems. Before proceeding, you will need to be sure of what you are going to do.
  • Compiling can take hours.
  • Some source packages do not come with configuration files or 'make' files. Then just type the 'make' command and see what happens.

Recommended: