Chapter 1. The Makefile.am Howto

Table of Contents

Makefile.am for a simple program
bin
*_SOURCES
*_LDADD
*_LDFLAGS
AM_CPPFLAGS
KDE_CXXFLAGS
METASOURCES
Makefile.am for shared libraries
*_LTLIBRARIES
*_LIBADD
*_LDFLAGS
Makefile.am for a plugin / module
Sharing code: convenience libs
Makefile.am for automated tests
Installing data
Uninstalling a desktop file
Icons
Other types of source files
Subdirs
Toplevel directories
Documentation
Translations
Qt-only program
Disabling "final" compilation
Don'ts
Turn Makefile.am into a Makefile

Makefile.am for a simple program

bin_PROGRAMS = kdialog

kdialog_SOURCES = kdialog.cpp widgets.cpp ...
kdialog_LDADD = $(LIB_KIO)
kdialog_LDFLAGS = $(all_libraties) $(KDE_RPATH)

AM_CPPFLAGS = $(all_includes)

METASOURCES = AUTO

bin

bin means you want to create something that will install into the bin directory of KDE. *_PROGRAMS means you want to compile a program. Use _SCRIPTS for scripts, etc. Then you can see the name of the program, which is what is used in the lines below to define what has to be done to create that program.

*_SOURCES

There you list all the sources.

*_LDADD

There you define which libraries the program links to. You can only use -lfoo, ../path/to/thelib.la (relative paths only), or the $(LIB_*) macros set by the kde build system. If a lib A depends on a lib B, you don't need to specify B. But if you do (e.g. because A's dependencies might change), make sure you specify A before B. So usually local .la libs are first, then $(LIB_*), then -lfoo if any.

*_LDFLAGS

Finally you define the flags to be passed to the linker, in program_LDFLAGS. This can be -L flags (usually set by a configure check into a variable). $(all_libraries) contains all the -L flags necessary to find Qt and KDE, you must have it there. For programs you should use $(KDE_RPATH), it helps getting programs to find their libs without having to set LD_LIBRARY_PATH.

AM_CPPFLAGS

additional compilation flags. This must contain $(all_includes). This is also where other compile-time flags like -DWITHOUTBUGS and -I$(srcdir)/subdir go. IMPORTANT: insert -I directives before $(all_includes). This ensures that your own headers will be used, not some older installed version.

Note that $(srcdir) and . (which is the build directory) are always included automatically. You might find that many Makefile.am files in KDE still use INCLUDES though, that's the old name for it, but AM_CPPFLAGS is the recommended way nowadays.

KDE_CXXFLAGS

There's also KDE_CXXFLAGS. Its role is to appended to the other flags, so it can undo one of the flags set by the build framework. This is the case for exceptions, which are disabled by default, so if you want to use exceptions you need to write

KDE_CXXFLAGS = $(USE_EXCEPTIONS)

METASOURCES

METASOURCES = AUTO
is the magic line that makes the KDE build system take care of the moc files automatically. It is the recommended way if all your .cpp/.cc files include their .moc file (this is the best way since it gives faster compilation), or if you are compiling a single binary/library.

In case you have multiple binaries/libraries in the same directory, you might need to use the longer form of

libsomething_la_METASOURCES = myfile.moc myotherfile.moc
mybinary_METASOURCES = someotherfile.moc