User Tools

Site Tools


programming:make:basic_make_example

This is an old revision of the document!


Programming - Make - Basic Make Example

Basic Example

Create a C program

hello.c
#include <stdio.h>
 
int main()
{
  printf("Hello World");
  return 0;
}

Create a Makefile

Makefile
hello: hello.c

Run Make

make

returns:

cc     hello.c   -o hello

NOTE: This compiles the C program.

Most distributions will have cc pointing to the default C compiler.


Run Make Again

make

returns:

make: 'hello' is up to date.

NOTE: As nothing has changed, there is no need for Make to recompile the C program.

To have the program be recompiled:

  • Run touch hello.c, or
  • Delete the compiled program, rm hello, or
  • Modify the C program and run make again.

Enhanced Make

Use variables within the Makefile:

Makefile
CC=gcc
CFLAGS=-g
hello : hello.c

returns:

gcc -g    hello.c   -o hello

Add further options to the Makefile

CC=gcc
CFLAGS=-g
# Comment next line to turn off optimization
CFLAGS+=-O
hello : hello.c

**NOTE: The implicit rule being used is:

$(CC) $(CFLAGS) $(CPPFLAGS) hello.c -o hello
programming/make/basic_make_example.1612449916.txt.gz · Last modified: 2021/02/04 14:45 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki