use autotools to make project

来源:互联网 发布:做小受的真实经历知乎 编辑:程序博客网 时间:2024/06/08 06:12

1.1. Write sources

  1. Create an empty directory called tut_prog and enter in it.
  2. In this new directory, create a new file named main.c containing:
Example 3-1main.c:
#include <stdio.h> int main(){printf("Hello world!/n");return 0;}

1.2. Run Autoconf

  1. Write the following in a file named configure.ac:
Example 3-2minimal configure.ac:
AC_INIT([Tutorial Program], 1.0)AM_INIT_AUTOMAKEAC_PROG_CCAC_CONFIG_FILES(Makefile)AC_OUTPUT

The configure template script could be named configure.in.It is the name used in older version (before 2001) of Autoconf.Nevertheless, it is recommended to use configure.ac because the .in extensionis already used by files processed by configure andgenerated by Automake: Makefile.in and autoheader:config.h.in.

AC_INIT, AM_INIT_AUTOMAKE, etc... are M4 macros. M4 is a macro expanding softwareused by Autotools; we don't need to know about it. When Autoconf will processthis configure.in, the macros will be expanded and we will get a fresh huge configure script.

AC_INIT

Is the first mandatory macro. We need to indicatethe name of the project and its version.

AM_INIT_AUTOMAKE

Initialize environment for Automake.It is needed in all projects using Automake.

AC_PROG_CC

Determine the C compiler to use.

AC_CONFIG_FILES

Create each file by copying the corresponding template file (with .in extension)and substituting the output variable values.

AC_OUTPUT

Marks the end of the configure template.

The use of some macros has changed between different versions of Autoconf:

  • The package name and version was defined as arguments of AM_INIT_AUTOMAKE instead of AC_INIT.
  • AC_OUTPUT was getting the list of generated files instead ofusing the additional macro AC_CONFIG_FILES.

Autoconf only knows its own macros but read additionalones in a file named aclocal.m4. These macros are used toextend Autoconf, it includes Automakemacro (starting with AM_) and other third party macros.For instance, if you develop a library called foo, you might want to write an AC_CHECK_FOR_FOOmacro so that developers using your library can check for its presence usingAutoconf.

aclocal scans configure.ac and create an aclocal.m4 file which contains the macros mentionedin configure.ac.aclocal is part of the Automakepackage and search by default in Automake macros andin a system path typically /usr/share/aclocal.

  1. Launch aclocal. It will create a new file namedaclocal.m4 in the current directory.
  2. Launch autoconf. It will create the configurescript configure.

On my system, I actually get an extra directory called autom4te.cache.That is for Autoconf internal purposes. You do not needto care about it.

1.3. Run Automake

  1. Write the following in a file named Makefile.am:
Example 3-3minimal Makefile.am:
bin_PROGRAMS = tut_progtut_prog_SOURCES = main.c

In Makefile.am are the very essential data needed to build the project:the target program, called tut_prog, will be put in a $prefix/bin/ directory;to build it we need main.c. Note that we don't specify howthat will be built: Automake will figure it out.We haven't even mentioned the compiler in this pre-makefile.

Makefile.am will be processed by Automake;the result will be a Makefile.in.This Makefile.in is close to being a real makefile, but it contains variable names whichwill be replaced when the configure script will run, resulting in a real makefile(called Makefile). For instance, configure will write in thefinal Makefile what compilerto use (it is the compiler it found using the AC_PROG_CC macro).

  1. Run the command automake --add-missing --foreign. It will createa new file named Makefile.in as expected. Moreover, due to theswitch --add-missing you get a fewlinks to scripts necessary for building the project: depcomp, install.sh and missing.The other option --foreign tells to Automake that you don't want to follow GNU standard and youdon't need mandatory documentation files: INSTALL,NEWS, README, AUTHORS,ChangeLog and COPYING. I have used it hereto keep the number of created file to a minimum but else it is a good idea toprovide these files, you can start with empty files.

1.4. Build project

  1. Run now the new configure script: ./configure. You getthe following output and it create the makefile for your program.

    checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output file name... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables... checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3configure: creating ./config.statusconfig.status: creating Makefileconfig.status: executing depfiles commands
  2. Run now make, to build your program. You get the followingoutput and a new tut_prog executable

    gcc -DPACKAGE_NAME=/"Tutorial/ Program/" -DPACKAGE_TARNAME=/"tutorial-program/"   /        -DPACKAGE_VERSION=/"1.0/" -DPACKAGE_STRING=/"Tutorial/ Program/ 1.0/"     /        -DPACKAGE_BUGREPORT=/"/" -DPACKAGE=/"tutorial-program/" -DVERSION=/"1.0/" /        -I.     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cmain.c: In function ‘main’:main.c:5: warning: return type of ‘main’ is not ‘int’mv -f .deps/main.Tpo .deps/main.Pogcc  -g -O2   -o tut_prog main.o
  3. Now, if you can write in /usr/local/bin, runmake install to install your program. Else you need tolog as root before or use sudo and run sudo make install.You should get.

    make[1]: Entering directory `/home/seb/Projects/Tutorial'test -z "/usr/local/bin" || /bin/mkdir -p "/usr/local/bin"  /usr/bin/install -c 'tut_prog' '/usr/local/bin/tut_prog'make[1]: Nothing to be done for `install-data-am'.make[1]: Leaving directory `/home/seb/Projects/Tutorial'

    Then, if /user/local/bin is in your path, you can runyour program from everywhere.

1.5. Clean project

  1. The program is installed, so you can clean the build directory runningmake clean. It removes all object files and theprogram but not the makefiles.

    test -z "tut_prog" || rm -f tut_progrm -f *.o

    You can still run the program installed in /user/local/bin.

  2. To remove the installed program, run make uninstall.Like for the installation, you need to use have the writing right in thedirectory or use su or sudo.

    rm -f '/usr/local/bin/tut_prog'

1.6. Generate project

Running aclocal, automake and autoconf one by one is fine for a tutorial tounderstand exactly what's happen. But, for a real work, it's a bit tediousespecially because there are other tools those could be needed likeautoheader, autopoint or libtoolize. After creating the project, themakefiles generated by configure should takecare of regenerating configure and allMakefile.in. Anyway, this lets a room for improvementand there are even two responses to this:

autoreconf

It is another tool part of the Autoconf packagewhich is running all scripts in the right order. To start a new project, youcan just run autoreconf --install and it will callall necessary commands.

autogen.sh

It is a script not part of Autotools, that it doingthe same thing. There is one named gnome-autogen.sh whichcomes with GNOME common development package but other project can write theirown ones.

原创粉丝点击