Because several spaces and a tab look much the same and because almost everywhere else in Linux programming there’s little distinction between spaces and tabs, this can cause problems. All rules must be on lines that start with a tab a space won’t do. The difference between a space and a tab. In the example in the previous section, what command should be used after the make command has determined that 2.o needs rebuilding? It may be that simply using gcc -c 2.c is sufficient.
MAKEFILE FOR C PROGRAM IN LINUX HOW TO
The second part of the makefile specifies the rules that describe how to create a target. You can see quite easily that, if b.h changes, you need to revise both 2.o and 3.o, and because2.o and 3.o will have changed, you also need to rebuild myapp. This set of dependencies gives a hierarchy showing how the source files relate to one other. Target 3.o is depends on 3.c,a.h,b.h,and c.h (dependencies for 3.o target) Target 2.o is depends on 2.c,a.h and b.h (dependencies for 2.o target) Target main.o is depends on main.c and a.h (dependencies for main.o target) This says that target myapp is depends on main.o,2.o and 3.o (dependencies for myapp target). The dependency list of the above program is as follows. In make file write these rules by writing the target,a colon, space or tab, then a tab or space separated list of files that are required to create a target file. Suppose a program contains below source files. Target is usually a executable file which get created after execution of make command. The dependencies specify how each file in the final application relates to the source files. If you want to call a file name XYZ, then you can call it by -f options. If that doesn’t exist, it looks for a file called Makefile. If you don’t use this option, the standard version of make looks first for a file called makefile in the current directory. You can use this, for example, to find out in one go which source files fail to compile.Ģ) -n, which tells make to print out what it would have done without actually doing it.ģ) -f ,which allows you to tell make which file to use as its makefile. The three most commonly used areġ) -k, which tells make to keep going when an error is found, rather than stopping as soon as the first problem is detected. The make program itself has several options. The make command uses the makefile to determine the order in which the targets have to be made and the correct sequence of rules to invoke. Often, other intermediate targets have to be created before the final target can be made. The makefile is read by the make command, which determines the target file or files that are to be made and then compares the dates and times of the source files to decide which rules need to be invoked to construct the target.
Typically, the target is a single executable file. The rules describe how to create the target from the dependent files. A dependency has a target (a file to be created) and a set of source files upon which it is dependent.
MAKEFILE FOR C PROGRAM IN LINUX INSTALL
It’s often used not only to control the compilation of source code, but also to prepare manual pages and to install the application into a target directory.Ī makefile consists of a set of dependencies and rules. The combination of the make command and a makefile provides a very powerful tool for managing projects.
Indeed, if you have a large project, you may choose to manage it using separate makefiles for different parts of the project. You can have many different makefiles on your machine at any one time. The makefile most often resides in the same directory as the other source files for the project. You must provide a file that tells make how your application is constructed.This file is called the makefile. Although, as you will see, the make command has a lot of built-in knowledge, it can’t know how to build your application all by itself.