crypto.bi – ELI5 Cryptography, cryptocurrency and programming

Create an autotools project from scratch

Recently I decided to finally try to understand autotools. It’s been on my list for ages, so I took the opportunity to read a bit more about the de-facto standard GNU build system. Cmake has been my C and C++ go-to build system for a while now, so learning Autotools is an opportunity to add one more tool to my belt.

In this post I’m sharing the routine I use to create an Autotools project from scratch. Whenever I create a new project, I just come back to this thread and copy/paste from here. I hope you find this useful.

Start New Project

To get started, you need to create configure.ac and Makefile.am

configure.ac

AC_INIT([YOURPROJECT], [1.0])
AC_CONFIG_SRCDIR([.])
AM_INIT_AUTOMAKE
AC_CONFIG_MACRO_DIRS([m4])
AC_PREREQ([2.69])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC
AC_CHECK_HEADERS([stdlib.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Makefile.am

AM_LDFLAGS=-lccoin -lglib-2.0
AM_CPPFLAGS = -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
ACLOCAL_AMFLAGS=-I m4 
bin_PROGRAMS=prog1
prog1_SOURCES=prog1.c
prog1_LDADD=-lglib-2.0 -lccoin -lpthread -lsecp256k1

Customize the libraries and include directories at will.

See below for details about the ccoin and secp256k1 libs used here.

Run autotools Commands

touch NEWS README AUTHORS COPYING ChangeLog INSTALL
aclocal
automake --add-missing
autoreconf
./configure
make

And Bob’s your uncle!

The above should generate all the basic files. From there on, all you gotta do is run make again. The automatically generated Makefile is smart enough to regenerate any modified files.

Building Outside the Main Tree

You may wish to build the project outside the main source tree, under a bin/ subdirectory for instance.

In that case, run the following commands.

make distclean
mkdir bin/
cd bin/
../configure
make

make distclean will clean up the cached configuration from your main source tree and get it back to factory settings. Then create a directory where you’d like to build the system, I chose bin/ arbitrarily. Enter the new directory and run the root dir’s configure from there. Autotools is smart enough to generate all the required build files in the new subdirectory.

Well I hope this short cookbook recipe has been useful. I keep coming back to this post whenever I create a new C project, so I hope it’s useful to you too!

Picocoin C Language Bitcoin Library

You may have noticed that I use picocoin and secp256k1 libraries in my Makefile.am. I use both these great libraries in order to play with Bitcoin programming! Picocoin allows me to create a tiny wallet, generate keys, addresses and so on programmatically with a very tiny footprint.

If interested in this library, clone it using this:

git clone --recurse-submodules -j8 https://github.com/jgarzik/picocoin.git

This will create a secp256k1 subdirectory where you’ll find the last missing library required by Makefile.am

Build it with the usual GNU procedure:

./configure
make -j8
sudo make install

Your build system should automatically use the /usr/local directory for builds. If it doesn’t, then please adjust your build environment accordingly.

Links

Introduction to GNU Autotools

Using Autotools

GNU Autotools: a tutorial

An Introduction to the Autotools

Autotools Tutorial Slides Presentation

Exit mobile version