Introduction: 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.
- A computer (obviously)
- A Linux system (this instructable will use Ubuntu 15.10)
Step 1: 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
Step 2: Directory Structure
For now, create a folder called 'a'.
In there, create a folder called src.
It should look like this:
Now, let's add a Makefile.am file to both directories.
Finally, add your source code to src.
Your directory structure is all set.
Step 3: '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:
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.
Step 4: '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
Step 5: 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:
mv configure.scan configure.ac
Now, let's edit the file.
Step 6: 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:
Then, you will see a line that says:
Delete this line.
Step 7: 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.
Step 8: 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
Step 9: That's All!
Hopefully I didn't confuse you too much...
We have a be nice policy.
Please be positive and constructive.