Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36
 

Exercise 28: Intermediate Makefiles

In the next three exercises you'll create a skeleton project directory to use in building your C programs later. This skeleton directory will be used in the rest of the book. In this exercise, I'll cover just the Makefile so you can understand it.

The purpose of this structure is to make it easy to build medium-sized programs without having to resort to configure tools. If done right, you can get very far with just GNU make and some small shell scripts.

The Basic Project Structure

The first thing to do is make a c-skeleton directory, and then put a set of basic files and directories in it that many projects have. Here's my starter:

$ mkdir c-skeleton
$ cd c-skeleton/
$ touch LICENSE README.md Makefile
$ mkdir bin src tests 
$ cp dbg.h src/   # this is from Ex20
$ ls -l
total 8
-rw-r--r--  1 zedshaw  staff     0 Mar 31 16:38 LICENSE
-rw-r--r--  1 zedshaw  staff  1168 Apr  1 17:00 Makefile
-rw-r--r--  1 zedshaw  staff     0 Mar 31 16:38 README.md
drwxr-xr-x  2 zedshaw  staff    68 Mar 31 16:38 bin
drwxr-xr-x  2 zedshaw  staff    68 Apr  1 10:07 build
drwxr-xr-x  3 zedshaw  staff   102 Apr  3 16:28 src
drwxr-xr-x  2 zedshaw  staff    68 Mar 31 16:38 tests
$ ls -l src
total 8
-rw-r--r--  1 zedshaw  staff  982 Apr  3 16:28 dbg.h
$

At the end you see me do an ls -l so that you can see the final results.

Here's:

  • LICENSE: If you release the source of your projects, you'll want to include a license. If you don't though, the code is copyright by you and nobody has rights to it by default.
  • README.md: Basic instructions for using your project go here. It ends in .md so that it will be interpreted as markdown.
  • Makefile: The main build file for the project.
  • bin/: Where programs that users can run go. This is usually empty, and the Makefile will create it if it's not there.
  • build/: Where libraries and other build artifacts go. Also empty and the Makefile will create it if it's not there.
  • src/: Where the source code goes, usually .c and .h files.
  • tests/: Where automated tests go.
  • src/dbg.h: I copied the dbg.h from Exercise 20 into src/ for later.

I'll now break down each of the components of this skeleton project so that you can understand how it works.

Previous Lesson Next Lesson

Register for Learn C the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.