Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Monday, October 5, 2015

Notes about writing a Makefile

This post contains notes copied from my project writeup that demonstrating how to write a Makefile on our own. 

When "make" is invoked, it builds the first target listed in the Makefile, which is typically the project executable in its default configuration.

A Makefile is a plain text file that contains a set of rules.

Each rule is of the form:
target: prerequisites ... commands to build target from prerequisites ...

If one of the prerequisite files specified by a rule doesn’t exist, make attempts to build that prerequisite from another rule that specifies the prerequisite as its target.
Once a target is built, it will not be rebuilt by subsequent invocations of make unless a prerequisite is modified (and, thus, making the target out of date).

"make" allows a Makefile to assign variables with the syntax “var = value”, and substitution of variables into rules with the syntax “$(var)”. Variable assignments may be overridden
on the “make” command line. For example, most Makefiles assign the CC variable to gcc as the default compiler and write compile rules using “$(CC)” to invoke it. If you wanted to substitute the default compiler with the avr-gcc cross compiler, you would execute “make CC=avr-gcc” instead of “make”.

"make" automatically assigns the variables $@, $<, and $^ when evaluating commands for a rule:$@ is assigned the file name of the target, $< is assigned the filename of the first prerequisite, and$^ is assigned a string consisting of the filenames of all the prerequisites with spaces between them. This feature allows the rule:

foo.o: foo.c common.h gcc -c -Wall -Werror -o foo.o foo.c

to be simplified to:

foo.o: foo.c common.h gcc -c -Wall -Werror -o $@ $<


A pattern rule is an ordinary rule that specifies a target, prerequisite, and commands for building the target, except that the file names for the target and prerequisite contain a wildcard (“%”) that matches at the beginning of the file name. Pattern rules define how to build files of a certain type. For example, the following pattern rule specifies how to build an object file from any C source-file:

%.o: %.c gcc -c -Wall -Werror -o $@ $<

Install Autoconf, Automake and Libtool on Mac OS X

I am currently working on a speech recognition related project and want to install an open source library that does the backend computation on my laptop. 

To install the library, aclocal, autoconf, automake and libtool are required. 

However, according to some blog articles, Mac OS X stopped to support auto tools ever since Xcode 4.4.1. 

Below are scripts I learned from other bloggers to manually install auto tools:

export build=~/devtools # or wherever you'd like to build
mkdir -p $build

cd $build
curl -OL http://ftpmirror.gnu.org/autoconf/autoconf-2.68.tar.gz
tar xzf autoconf-2.68.tar.gz
cd autoconf-2.68
./configure --prefix=$build/autotools-bin
make
make install
export PATH=$PATH:$build/autotools-bin/bin

cd $build
curl -OL http://ftpmirror.gnu.org/automake/automake-1.11.tar.gz
tar xzf automake-1.11.tar.gz
cd automake-1.11
./configure --prefix=$build/autotools-bin
make
make install

cd $build
curl -OL http://ftpmirror.gnu.org/libtool/libtool-2.4.tar.gz
tar xzf libtool-2.4.tar.gz
cd libtool-2.4
./configure --prefix=$build/autotools-bin
make
make install