Welcome to Autotools: a Portability Solution for Linux Systems
by InitializeSahib in Circuits > Computers
1464 Views, 7 Favorites, 0 Comments
Welcome to Autotools: a Portability Solution for Linux Systems
Any C or C++ programmer wants to make their software as simple as possible.
And that means portability.
GNU make (future instructable) seems like a good option, right?
Until you realize the Makefiles are platform-dependent.
This instructable will show you the GNU build system at its core.
Requirements:
- A computer (obviously)
- A Linux system (this instructable will use Ubuntu 15.10)
Prepare
Unless your distro was designed for this, it probably does not have development tools installed.
Using your package manager, install the GNU Compiler Collection (probably already installed), autoconf, automake, libtool, fakeroot, and everything from your distro's dev package.
(If this seems confusing, a GUI package manager may help.)
For Ubuntu systems (run this in the terminal): sudo apt-get install autoconf automake libtool fakeroot build-essential
Directory Structure
For now, create a folder called 'a'.
In there, create a folder called src.
It should look like this:
a:
src:
Now, let's add a Makefile.am file to both directories.
a:
Makefile.am
src:
Makefile.am
Finally, add your source code to src.
a:
Makefile.am
src:
Makefile.am
a.cpp
Your directory structure is all set.
'Making' the Makefile: Part 1
Let's now work on the root Makefile.am (the one in the directory above src).
Open it in whatever text editor you want, and then type inside of it:
AUTOMAKE_OPTIONS=foreign
SUBDIRS=src
Then, save and close it.
Let me explain:
The AUTOMAKE_OPTIONS macro tells Automake what options to use. Specifying "foreign" means that our project does not follow the GNU format. If we did not say this, Automake would do lots of unnessecary and harmful work. (If it does follow the GNU format, you can remove it.)
The SUBDIRS macro tells Automake that it should look for Makefiles in the 'src' directory.
'Making' the Makefile: Part 2
Now, let's work on the Makefile.am inside of the src directory.
In there, type this:
bin_PROGRAMS = a
a_SOURCES = a.cpp
Save and close it.
bin_PROGRAMS tells us that there is a program called 'a', and it needs to be installed in $(PREFIX)/bin.
$(PREFIX) is a variable defined when you run ./configure, which we will get into later.
a_SOURCES tells us the program a has a source file called a.cpp
ALL YOUR SCAN ARE BELONG TO US
Get that reference? ;)
We will now introduce a new file, configure.ac
This is going to be located in your project's root directory (the directory above src).
Instead of manually writing it, there is a neat little utility that does it for us: autoscan.
Run these commands in the terminal:
autoscan
mv configure.scan configure.ac
rm autoscan.log
Now, let's edit the file.
Configuring the Configure
You will see a line that looks like this:
AC_INIT([full package name], [version], [bug report address])
Replace [full package name] with the project name surrounded in brackets (e.g [a])
Replace [version] with the project version surrounded in brackets (e.g [1.0])
Replace [bug report address] with your email address, surrounded in brackets.
Right after the AC_INIT line, add a line that says:
AM_INIT_AUTOMAKE
Then, you will see a line that says:
AC_CONFIG_HEADERS([src/config.h])
Delete this line.
Distribution
Now that you are done with that, go to the root directory of your project, and run this command:
aclocal && autoconf && automake --add-missing
What does this do?
aclocal generates the cache autoconf needs.
autoconf will generate the ./configure script, which is needed to generate the Makefiles.
automake --add-missing will add the missing shell scripts, and will create the Makefile.in files that are needed by ./configure.
At this point, your source code is ready for distribution.
Compiling
Run this command:
./configure && make
This will generate the Makefiles, then make the project.
To install the project:
./configure && make && sudo make install
To install it in /a/bin:
./configure PREFIX=/a/ && make && sudo make install
That's All!
Hopefully I didn't confuse you too much...