Saturday, June 06, 2009

Tutorial: using Autotools with Eclipse (and Linux Tools Project)

As a GNU maintainer, I've always been using autotools (i.e., automake, autoconf, etc.), and I must admit I still haven't found anything such powerful. Now, I started using the plugins of the Linux Tool Project which seem to have improved a lot! They provide plugins for ChangeLog, Valgrind, and in particular for autotools based projects. Since I haven't seen any tutorial for using the autotools plugin, I thought I could write one; well, it's not that difficult to use the autotools plugin, but I hope it can help :-)

Of course, first of all, you need to install the autotools plugin, and I personally prefer the update site method, using Remember that the linux tools are based on CDT plugin.

For this tutorial, we will use an already autotools based project (for the moment, the autotools plugin can create an autotools based project, but without any templates). We will use the GNU Hello program, so you can download the last version from the web site. Then you can untar the contents in a directory (say tmp).

Now, it's time to create a new project in eclipse, in particular, a GNU C Autotools Project (or a C++ project if you're project uses C++):

Let's call it "hello" (and then you can select your location and accept the other defaults). You will now have an empty autotools based C project.

Now, let's import the hello package from the directory where we had untarred it, by using "import file system":

Now, the hello project is populated with the package of GNU Hello; notice that, and .m4 files are handled by the autotools plugins with syntax highlighting, outline, mouse over functionalities, etc.:

Now, BEFORE building the project for the first time, let's take a look at the properties of the project, where you can see we now have an autotools part, where you can customize the autotools versions you might want to use (I take the defaults):

In particular, from "C/C++ Build - Settings" you can configure the arguments passed to the tools, and the build directory (it is often advised to build a package in a different directory, so that you can configure different builds; by default, the build directory is "build", and I'd leave this that way):

Now, let's change some options! For instance, the configure's --prefix option:

The suffix of the installed program

Since we build the program for development and for debugging, let's make sure to turn off C compiler optimizations (which make debugging a pain):

You can then check that debugging is already turned on, as also '-Wall' option (to report all warnings).

Since we downloaded the source package of hello, produced by 'make dist', this provides all the scripts generated by the autotools in order to run 'configure && make'. However, if you get a program from the CVS, it is likely that you need to bootstrap autotools. Now, the autotools plugin relies on an '' script which calls all the autotools in the right order, which must be provided by the package itself; however, this is kind of an old method, since now, to bootstrap autotools, you can rely on the program 'autoreconf' which is part of autotools; so, in case you need to bootstrap the autotools, you can simply call 'autoreconf -i' (this will install required files from autotools) from a shell, in the directory of the project.

Now, you can build the project, by right clicking on the project and by choosing "Build Project"! Since this is the first time we build it, the build directory will be created, the configure script will be called with all the features we selected, and the files are compiled!

Hey, but wait a minute! We could have used ccache to compile the sources! And we should have also enable maintainer mode specific configuration settings! Well, we can still do that by going back to the settings of the project, enable the maintainer setting check box, and specify the gcc command as 'ccache gcc':

Now, let's clean the project (this will issue a 'make distclean', but you can customize this from the setting), and build it again, so that it will be reconfigured with the new settings.

Now, let's create another build configuration (e.g., for the release version of the package, with optimized compiler options, with a different prefix, removing the suffix '-debug' and without the maintainer-mode option enabled). We just need to go to "Manage Configurations...", create a new one, starting from the previous default one:

and now we can set the optimization level for the compiler, disable maintainer mode and use another --prefix option (e.g., '/usr/local/'). It is also important to specify another build directory, say 'build_release' (but when creating a new configuration, the build directory has already been changed to something different from the previous one, i.e., 'build1'):

Now, we can switch between the two different build configurations:

Well, that's all for now; I hope you find this tutorial useful :-)


Andrew Overholt said...

Thanks for writing the tutorial! We'll have to write something similar to have as documentation.

betto said...

If I can help with the documentation I'd be happy! :-)

unfortunately, there seems to be a bug with optimization options, see bug #279362

Anonymous said...

Thanks for writing this tutorial; I was looking for such a help since a long time. I did several trials but have always been confused with this plenty of options and could not figure how to make relations with the autotools I was used to.


Magnus said...

For latest version of Eclips(aka Gallileo), see this

Anonymous said...

I found the tutorial useful however I'm not familar with eclipse at all. That meant I had a lot of trouble locating the dialogs you displayed. I spent a lot of time hunting. For example, it took me some time to realize how to get the GCC C Compiler settings into the Tool Settings tab. I didn't enjoy the seemingly senseless, uneasily closed popups either (like the one for "from"). It meant a lot of extra care when moving the mouse over the text.

Still I learned something. Thanks!

ofer_brown said...

there is the old way of:

Anonymous said...

An excellent tutorial.
I was wondering if it would be possible to extend it into covering libtool projects ?

betto said...

do you mean to extend the tutorial or the eclipse plugin?

Anonymous said...

I meant the tutorial, but the research I've done recently leads me to believe the plug-in probably needs some work to support libtool.

If you have some tips for getting libtool to work with the current or 3.4 version of Eclipse I am sure there are many who would be grateful.

betto said...

I see, but anyway, the plugin does not provide any wizard at the moment, thus you must write all the autotools stuff (including libtool stuff) yourself... thus, this tutorial assumes one already knows about autotools.

Is that what you meant?

Anonymous said...

Not really.
I understand that a working knowledge of autotools is required. The problem is that when a project includes both an executable and a shared library the support in Eclipse for libtool seems a bit absent.
See for example.

I have developed a rough work-around, buts its not well tested - see

Thanks for your interest - its much appreciated.

hoglet said...

Thanks for a great tutorial, it got me (almost) going. Debugging is horrible though because I can not disable optimization

I wonder how to get the GCC C Compiler settings into the Tool Settings tab?

I am using Galileo/CDT 6.0/Linux tools 0.3 and there simply is not any such setting. Have they taken it away?

betto said...

Hi, glad you found the tutorial useful.
When I wrote it, I hadn't realized that disabling optimizations from the settings menu did not work.
But you can set the CXXFLAGS environment variable (in the C/C++ build settings) to, for instance, "-g -O0" (or CFLAGS if you're using C).
Please let me know whether this works

hoglet said...

YESSS! It seems to work, now I have a great environment and I do not have to switch between eclipse and anjuta all the time.

Many, many thanks!

Anonymous said...

Grazie mille! Excellent tutorial!

But I have a problem. The C/C++ Build cannot be found in the Properties. Can it be hidden/disabled? If so, how can I enable it back?


betto said...

Probably it is now part of "Build" general property menu? I cannot test it right now...

John said...

Actually there's no C/C++ Build section also, only C/C++ Documentation, C/C++ File Types, and C/C++ Indexers.

Also, I have 2 projects, I tried deleting the GNUmakefiles and the 1st project can re-generate the GNUmakefiles but the 2nd project cannot. Is there an option to set the automatic re-generation of the makefiles?

betto said...

I meant that the builder should be in the builders section... have you created the project as a C/C++ project?