isakbm / make_tutorial

A simple example C project using Make

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make Tutorial

This is essentially a copy of the 5th example from colby.

It is a very simple C project that contains the following files

  • hellomake.c
  • hellofunc.c
  • hellomake.h

Make is used to automate the compile and linking and makes it easy to organize the project and its dependencies.

Lets have a look at the directory structure using tree, if you don't have tree

sudo apt-get install tree 

Here's what it should look like

$ cd make_tutorial
$ tree
.
├── include
│   └── hellomake.h
├── lib
├── README.md
└── src
    ├── hellofunc.c
    ├── hellomake.c
    ├── Makefile
    └── obj

4 directories, 5 files

Now let's run make from within the src directory and see what happens

$ cd make_tutorial/src
$ make
gcc -c -o obj/hellomake.o hellomake.c -I../include
gcc -c -o obj/hellofunc.o hellofunc.c -I../include
gcc -o hellomake obj/hellomake.o obj/hellofunc.o -I../include -lm 
$ tree
.
├── hellofunc.c
├── hellomake
├── hellomake.c
├── Makefile
└── obj
    ├── hellofunc.o
    └── hellomake.o

1 directory, 6 files

We now have two compiled object files and the target executable hellomake

$ ./hellomake
Hello makefiles!

Now let's remove all the intermediate object files. This is useful if you're done development and don't want to waste space.

$ make clean
rm -f obj/*.o *~ core /*~ 
$ tree
.
├── hellofunc.c
├── hellomake
├── hellomake.c
├── Makefile
└── obj

1 directory, 4 files

That's it, now adjust it to your own project and learn more about Make by reading the man pages.

About

A simple example C project using Make


Languages

Language:Makefile 56.7%Language:C 34.6%Language:C++ 8.7%