Вы находитесь на странице: 1из 3

Makeles

Jason Mercer 2007


Makeles are a list of rules that aids in any multi-step process. Traditionally they are used in software development projects but they could just as easily be used for websites, documents or backups. The advantages of a Makele over a script is that it can gure out if a set of commands actually needs to be run and you can select, from the prompt, specically which action you wish to trigger. The UNIX command make operates on a given makele, or the le in the current directory named Makele or makele by default. Lets have a look at a simple Makele. This makele can compile program.f90 into an executable le called program or it can delete the compiled program and all temporary les. FC=f90 NAME=program all: program.f90 ${FC} program.f90 -o ${NAME} clean: rm rm rm rm -f -f -f -f ${NAME} $ *.o *.mod *~

Any line of the form a=b is called an Assignment or a Macro. These are generally at the top of the Makele and provide exibility. We have declared that FC is equal to f90 and later we use this variable to compile a Fortran program. The notation ${NAME} or $(NAME) is used to extract the value held in a variable. We could switch the compiler for our project by changing a single line. For instance, we could use the NAG Fortran Compiler (FC=f95nag), the Intel Fortran Compiler (FC=ifort), Portland Group (FC=pgf90) or any others as long as they are installed on the system. Likewise, we have dened a variable called NAME which will be the name of the executable of our program. In this example, after the assignments, we dene two targets. The rst target, all, is the default target which will be called if no arguments are supplied when make is called. A target name must be followed by a colon, an optional set of dependencies and an optional body. The body starts on the line immediately below the target name. Each line in the body must start with the tab character (which looks a lot like 8 spaces). The all target checks to see if the program le exists (program.f90 in this case) and if it does, it will compile it. The

clean target removes any temporary les, object les and executable les which make all may have created. Usually a make clean will remove anything that a make all created. These target names, all and clean, are the traditional names but any names could have been used. A target is said to have failed if any element of its body failed. When a target fails, the entire make process fails. This is useful when compiling multiple les in a project since the compilation will halt when the rst error is found. This halt on failure is why we have added the -f argument to the remove commands in clean this forces the remove command to succeed, even if no les exist. We wouldnt want make clean to fail just because we dont have any temporary les in the directory ending with the tilde(~). Targets can be patterns rather than constant le names. Consider the following Makele. This makele compiles two c source les, hello.c and main.c into a single executable le called c_test when the all target is invoked. CC=gcc OBJECTS=hello.o main.o LIBS=-lm NAME=c_test all: ${NAME} ${NAME}: ${OBJECTS} ${CC} ${LIBS} ${OBJECTS} -o ${NAME} main.o: main.c ${CC} -c main.c -o main.o %.o : %.c ${CC} -c $*.c -o $*.o clean: rm -f *~ rm -f ${OBJECTS} rm -f ${NAME} The % is a wildcard and the pattern %.c will match any les ending with .c. Here we see a target of the form %.o with a dependency on %.c. This makes sense, if you want an object le, you would expect to need a source le of the same name. When we actually compile the source le we get access to the matched pattern with the special variable $*. One of the extra variables in this Makele over the last is the LIBS variable. This is commonly used to dene the path (with -L/path/name) and name (with -lname) of an external library, in this case we are linking against the standard math library (-lm). Math routines such as square roots and trigonometry are automatically included during a Fortran compile while they must be explicitly added, through the use of libraries, to a C program. This dierence is present because of the intended uses of each language. Fortran has become a numerical computation or scientic computing tool. C can also be used in this environment, but it is also used in kernel programming (where oating point operations 2

are prohibited) and embedded hardware (where oating point operations may not exist). Neither Fortran nor C include every type of mathematical operation so it is often required to add functionality through external libraries. Another useful aspect of Makeles is how dependencies are satised. Make considers a dependency satised if the target name is newer than the dependency. In the above example, main.o would be compiled only if a) it doesnt exist, or b) main.c has a modication date more recent than main.o. Again, this seems reasonable, there is no need to recompile main.o if main.c has not been changed since main.o was last created. This is all that is needed to create a simple Makele. They can become very complicated and many vendors have custom extensions, a good point to start for more information is http://www.gnu.org/software/make/. Examine the makeles in each of the directories which accompany this document, they will cover most of the topics that will be required in this course and include features beyond these two examples.

Вам также может понравиться