Tip 1: use a makefile

01 October 11. [link] PDF version

Part of a series of tips on POSIX and C. Start from the tip intro page, or get 21st Century C, the book based on this series.

level: Start here
purpose: Worry about compilation details once and only once

On the scale from works out of the box on one end to infinitely configurable and tweakable on the other, the C compiler is far on the tweakability side. But correctly tweaking the compiler is a problem you need to solve once, and then get on with your life. The solution is the makefile, which is basically an organized set of variables and shell scripts.

If you just want to go from .c files to an executable in least time, here's the makefile for you:

CFLAGS = -g -Wall -std=gnu99 -O3  
LDLIBS=
OBJECTS=
CC=gcc

$(P): $(OBJECTS)

Usage:

Tweaks:

OK, you're done! After you export P=your_program, you can run make and watch the compiler run and/or spit out errors, and never worry about what all that junk in the makefile actually means.

There will be limited plugs for Modeling with Data in this tip-a-day series, but I think it's worth mentioning that Appendix A goes into great detail about tweaking the makefile to do great things. Some notes for now:

To do:
Here's the world-famous hello.c program, in two lines:

#include <stdio.h>
int main(){ printf("Hello, world.\n"); }

Save that and the makefile to a directory, and try the above steps to get the program compiled and running.


[Previous entry: "Tip-a-day mode"]
[Next entry: "Tip 2: Use libraries"]