A Simple Make file tutorial

Source: Internet
Author: User


A Simple Makefile Tutorial

Makefiles is a simple-to-organize code compilation. This tutorial does isn't even scratch the surface of what's the possible using make, but was intended as a starters guide so tha T can quickly and easily create your own makefiles for small to medium-sized projects.
A Simple Example

Let's start off with the following three files, hellomake.c, HELLOFUNC.C, and hellomake.h, which would represent a typical Main program, some functional code in a separate file, and an include file, respectively.
HELLOMAKE.C HELLOFUNC.C hellomake.h

#include

int main () {
Call a function in another file
Myprinthellomake ();

return (0);
}

#include <stdio.h>
#include

void Myprinthellomake (void) {

printf ("Hello makefiles!\n");

Return
}

/*
Example include file
*/

void Myprinthellomake (void);

Normally, would compile this collection of code by executing the following command:

Gcc-o hellomake hellomake.c hellofunc.c-i.

This compiles the "C files" and names the executable hellomake. The-i. is included so, GCC would look in the current directory (.) for the include file hellomake.h. Without a makefile, the typical approach to the Test/modify/debug cycle are to use the "up" arrow in a terminal to go Your last compile command so you don ' t has to type it each time, especially once you ' ve added a few more. c Files to the Mix.

Unfortunately, this approach-compilation has a downfalls. First, if you are lose the compile command or switch computers you had to retype it from scratch, which was inefficient at BES T. Second, if you were only making changes to one. c file, recompiling all of them every time was also time-consuming and in Efficient. So, it's time to see how we can do with a makefile.

The simplest makefile you could create would look something like:
Makefile 1

Hellomake:hellomake.c HELLOFUNC.C
Gcc-o hellomake hellomake.c hellofunc.c-i.

if you put the this rule into a file called Makefile or makefile and then type do on the command line it would execute the compile command as you had written it in the MAKEF Ile. Note that do with no arguments executes the first rule in the file. Furthermore, by putting the list of files on which the command depends on the first line after the:, make knows the Rule Hellomake needs to is executed if any of the those files change. Immediately, you have solved problem #1 and can avoid using the-arrow repeatedly, looking for your last compile command . However, the system is still not being efficient in terms of compiling only the latest changes.

One very important thing to note are that there are a tab before the GCC command in the makefile. There must is a tab at the beginning for any command, and make would not be happy if it's not there.

In order-to-be-a bit more efficient, let's try the following:
Makefile 2

cc=gcc
Cflags=-i.

HELLOMAKE:HELLOMAKE.O HELLOFUNC.O
$ (CC)-O hellomake hellomake.o hellofunc.o-i.

Using This form of makefile is sufficient for the most small scale projects. However, there is one thing Missing:dependency on the include files. If you were to make a change to hellomake.h, for example, do would not recompile the. c files, even though they needed t o Be. In the order to fix the, we need to tell make, and all. c files depend on certain. h files. We can do this by writing a simple rule and adding it to the makefile.
Makefile 3

cc=gcc
Cflags=-i.
DEPS = hellomake.h

%.O:%.C $ (DEPS)
$ (CC)-c-o [email protected] $< $ (CFLAGS)

HELLOMAKE:HELLOMAKE.O HELLOFUNC.O
Gcc-o hellomake hellomake.o hellofunc.o-i.

As a final simplification, let's use the special macros [email protected] and $^, which is the left and right sides of th E:, respectively, to make the overall compilation rule more general. In the example below, all of the include files should is listed as part of the macro DEPS, and all of the object files Sho Uld is listed as part of the macro OBJ.
Makefile 4

cc=gcc
Cflags=-i.
DEPS = hellomake.h
OBJ = HELLOMAKE.O HELLOFUNC.O

%.O:%.C $ (DEPS)
$ (CC)-c-o [email protected] $< $ (CFLAGS)

Hellomake: $ (OBJ)
Gcc-o [email protected] $^ $ (CFLAGS)

makefile 5

Idir =.. /include
cc=gcc
cflags=-i$ (Idir)

Odir=obj
Ldir =.. /lib

Libs=-lm

_deps = hellomake.h
DEPS = $ (patsubst%,$ (idir)/%,$ (_deps))

_obj = HELLOMAKE.O HELLOFUNC.O
OBJ = $ (patsubst%,$ (ODIR)/%,$ (_obj))


$ (ODIR)/%.O:%.C $ (DEPS)
$ (CC)-c-o [email protected] $< $ (CFLAGS)

Hellomake: $ (OBJ)
Gcc-o [email protected] $^ $ (CFLAGS) $ (LIBS)

. Phony:clean

Clean
Rm-f $ (ODIR)/*.O *~ core $ (incdir)/*~

So now you have a perfectly good makefile the can modify to manage small and medium-sized software projects. You can add multiple rules to a makefile; You can even create rules. For more information on makefiles and the Do function, check out the GNU make Manual, which'll tell you more than you Ever wanted to know (really).

A Simple Make file tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.