My First Makefile

The following is a sample Makefile for simple project, here I am building a simple “Bloom Filter” library, and “main” program to use it. The library will have the following files:
* hash.h and hash.c
* bloom.h and bloom.c and we should get “libbloom.a” out of it. The “main” program will use:

  • main.c
  • bloom.h
  • libbloom.a and we should get “main” executable. So our make file should look like this:
$ cat Makefile
CFLAGS=-Wall -O3

CFLAGS += `pkg-config --cflags libpcre`
LDFLAGS += `pkg-config --libs libpcre`

OBJS=main.o other.o libbloom.a
BLOOM_OBJS=hash.o bloom.o

all: main libbloom.a

main: $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

libbloom.a: $(BLOOM_OBJS)
        ar rcs $@ $^

.PHONY: clean
        -rm main libbloom.a *.o 

Makefile use TAB not spaces for indentation. To execute it just type “make” in the same directory where “Makefile” reside. Macros Explained:

2 thoughts on “My First Makefile”

  1. Not sure why dynamic language needs it, you don’t have to compile anything?!

    BTW, I am trying to switch to CMake instead of plain Makefile, check it out if you have compiled projects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.