Makefile Instructions

From Kietzman.org

Jump to: navigation, search

Introduction

Makefiles are used to prepare applications to run in the system. They have many similarities to working with shell as you will see. A Makefile's functionality comes from its ability to follow labels according to their dependencies.

The Makefile should reside in the same directory as the source code.

Capabilities

  • Check and fetch dependencies.
  • Compile source code into object code.
  • Compile object code into binary executables.
  • Build test suites.
  • Install binary executables into the system.
  • Clean the source directory.
  • Uninstall binary executables out of the system.

There are many flavors of Makefiles, but for the most part they all function using the same command-line calls.

Typical Commands

make
make install
make uninstall
make clean

Basic Example

BIN=bin/
INSTALLBIN=/sys/app
OBJ=obj/
LD_LIBRARY_PATH=/usr/lib:/usr/local/lib
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

all: $(BIN)app

$(BIN)%: $(OBJ)%.o
  -if [ ! -d $(BIN) ]; then mkdir $(BIN); fi
  g++ -o $@ $<

$(OBJ)%.o: %.cpp
  g++ -Wall -ggdb -c $< -o $@

install: $(BIN)app
  -if [ ! -d $(INSTALLBIN) ]; then mkdir -p $(INSTALLBIN); fi
  install --mode 775 $(BIN)app $(INSTALLBIN)

uninstall:
  -if [ -d $(INSTALLBIN) ]; then rm -fr $(INSTALLBIN); fi

clean:
  -rm -fr $(BIN)
  -rm -fr $(OBJ)
Personal tools