5

I have a moderate C++ project. I'm trying to use autotools for it, but find the complexity overwhelming.

What are guidelines for when to use autotools and when you can do without it? What are (simple) alternatives?

The main reason I want to use autotools is for its full make install support. Is there a simpler alternative?

Ideally, I'd like something supported by Eclipse CDT.

SRobertJames
  • 8,210
  • 14
  • 60
  • 107

4 Answers4

7

For the make install support, you only need automake. And a simple Makefile.am file is quite easy to make:

LIBS += -lsome-lib -lsome_other_lib

bin_PROGRAMS = hello

noinst_HEADERS = some.h header.h files.h

hello_SOURCES = hello.c some.c other.c source.c file.c

That's about it.


The autoconf tool is useful if you want to make your program more platform-independent. You write a simple script to test the existence of the system header files and system libraries you use. If not found you either give an error or use copies provided in your package.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
4

For trivial, self-contained (no dependencies, nothing to be installed), projects with up to 3 source code files, write a Makefile:

.PHONY: all clean

all: program

clean:
    rm -f *.o

program: sourceA.o sourceB.o
    $(CXX) -o $@ $^ $(LDFLAGS)

You can define variables for CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, at least GNU make fills in the blanks in the expected way.

It of course doesn't address the problem of dependency tracking, nor checking for system capabilities. A common problem for non-trivial projects is to interact with the system: making sure certain libraries are usable, and installing/uninstalling. Most tools fail on both.

Here's a few examples I have experience with:

Premake

It generates consistent project files for different IDEs (and also Makefiles if you just need to build the code). Doesn't do anything else (no install target, no way to check for availability of libraries). Only useful if you need to supply project files for an IDE that you don't use (e.g. you use Eclipse and somebody else wants to compile it on Visual Studio). Anything non-trivial requires LUA scripting.

CMAKE

If all you need is to call the compiler to process your files, it's acceptable. For almost anything you'll have to memorize (or copy/paste) sequences of low-level macros (which form a programming language of sorts). Finding libraries on the system is convoluted and messy (some libraries were blessed by the creators and have hard-coded tests to find them, so you might be lucky). Considering the amount of broken cmake scripts I had to deal with to this date, I think most people just copy/paste the macros around without trying to understand it. Can install, but not uninstall. With some cmake scripts you might have to run cmake multiple times until it generates the correct output (e.g. VTK).

SCons

Seems to be CMAKE and Premake done better: no macros, uses a well-known programming language (Python) to provide a fair amount of useful functionality. Still fails in a number of ways; specifying a non-hardcoded installation prefix requires non-trivial effort, because it's not built-in.

Autotools

Does far more than the tools mentioned above. And most of them, conveniently. Has sane defaults; some highlights:

  • AC_CHECK_LIB(foobar, function_to_check_linking) - this finds a library named foobar, and put it in the $LIBS environment variable. Yes, detecting libraries is an important common task; if your tool doesn't consider this a first-class use-case, ditch it. (It's more convenient to use PKG_CHECK_MODULES though.)

  • Every action during ./configure is logged in config.log, so you can actually figure out what went wrong. For most other tools, at best you'll get "Boost_dir-NOTFOUND".

  • Already comes with built-in make install, make uninstall (what do you mean, your tool can put stuffs on my system but can't remove them?), make check (if you specified test_ programs), make dist-gzip (packages the source files into a tar.gz), make distcheck (creates a tar.gz and makes sure everything builds correctly and all tests pass). Even better, it plays nicely with checkinstall so you can create and distribute .rpms and .debs from it.

  • You can mix in good old Makefile rules inside automake Makefiles if needed.

  • Autotools files are tracked as well as source files, so helper scripts and Makefiles are generated again if needed, by simply invoking make.

Yes, it's "hard" to learn, but no more than learning all the specific macros/functions to call in CMAKE/SCons, in my opinion. An initial Makefile.am for a project is just a list of files assigned to a variable, and an initial configure.ac can be generated by autoscan; I find it convenient even for more trivial projects.

The autobook is the best source I know to learn it, but it's unfortunately outdated (autoconf will complain a lot about using deprecated macros).

DanielKO
  • 4,422
  • 19
  • 29
  • good answer, just one comment about PKG_CHECK_MODULES: it's evil, don't use it: http://tirania.org/blog/archive/2012/Oct-20.html – knocte Jan 19 '14 at 12:10
  • Is Miguel still relevant? The only problem he pointed out can be trivially solved by either distributing properly packaged tarballs (with configure already generated) or copying pkg.m4 into the project (and let autoconf versioning kick in). The latter is not that useful since without pkg-config the user will have to specify all paths and flags by hand in the _CFLAGS and _LIBS variables. – DanielKO Jan 19 '14 at 20:22
  • WRT relevancy: that is ad hominem fallacy. WRT "by hand", have you read the entire blog post? It clearly says that saving 4 lines of code (the ones required to set CFLAGS and LIBS) don't justify all the complexity of ACLOCAL and friends. – knocte Jan 19 '14 at 23:56
  • He is very loud and some times misguided, that blog post shows it. Yes, I read it, he doesn't state directly what the problem is, and he doesn't seem to have a clue on what PKG_CHECK_MODULES does. Hint: it allows you to suppress pkg-config invocation by supplying your own override; it has informative error messages; it checks that pkg-config exists before claiming a module doesn't exist. Those 200 lines in pkg.m4 have quite a bit of error messages and comments; give me precise error checking and diagnosing "bloat" any time, over a couple of lines that can fail silently. – DanielKO Jan 20 '14 at 18:22
  • your comment is almost the same as one of the comments in his blog post, and Miguel replies to it, and I think his answer is a valid answer for your comment right here too – knocte Jan 20 '14 at 18:30
  • I don't see how my comment is similar, and I don't see how Miguel's answer is any good, since he just called Emmanuele's comment stupid, moronic and a display of idiocy. Your comments are mostly devoid of content too; the macro in question has a lot of functionality that can come in handy; Miguel is conveniently ignoring this in favor of a quick hack that only works in cases where the problem he pointed out int he Google search doesn't occur. Users are not supposed to install from cloned repositories, they are supposed to install from release tarballs. Autoconf is not a tool for end-users. – DanielKO Jan 20 '14 at 21:50
2

You can pick and choose the parts of autotools that you want to use. Many projects use only autoconf, the part of autotools that generates a configure script, and if you just want to generate a Makefile with an install target that is configurable by the end user, then that's probably all you need.

caf
  • 233,326
  • 40
  • 323
  • 462
0

I don't know if this is too far off answer-wise, but I'd consider scons. It takes a bit to get going, but does many, many things automatically that has to be hand-teased with make.

Jiminion
  • 5,080
  • 1
  • 31
  • 54