Logo Download Perl Now!

User's Guide - tmake



Latest beta: 2.13-beta1

Latest release: 2.12


tmake is an easy-to-use tool to create and maintain makefiles and IDE project files for software projects. It can be a painful task to manage makefiles manually, especially if you develop for more than one platform or use more than one compiler. tmake automates and streamlines this process and lets you spend your valuable time on writing code, not makefiles.

tmake was originally developed by Trolltech to autogenerate Makefiles for building the cross-platform GUI toolkit Qt. It has evolved to support around 15 different C++ compilers integrating with build tools such as moc, as well as generating IDE project files for Microsoft Visual Studio. It automatically deals with operating system specific details such as linkage libraries and pathing ideosyncracies across a wide range of operating systems including 15 flavors of Unix, Microsoft Windows, Apple Mac OS X, and the Cygwin/MinGW win32 unix environments. It also supports embedded systems and processors including the arm, ipaq, sharp, cassiopeia, koala, mips, and qnx-rtp.

tmake is written in Perl and requires that you have installed perl version 5 or newer. Basic use of tmake requires no perl knowledge, but if you know perl you can extend tmake and write your own makefile templates.

For a full listing of features and options available, please look at the tmake reference manual. The available tmake output types are listed in the tmake output manual. Enhancements added to each new version of tmake are listed in this distribution's CHANGES file.

tmake is free software and you may use, copy, modify and distribute tmake and its documentation for any purpose and without any fee. See the LICENSE file for details.

Feedback is highly appreciated. Contact the current maintainer, Geoff Brimhall (, if you have ideas, patches etc. for tmake.


  1. Make sure you have perl version 5 or later installed. Microsoft Windows users can download perl for Win32 (Windows 9x, NT, 2k, and XP) from
  2. Unpack the tmake-<version>.tar.gz archive for Unix or the tmake-<version>.zip file for Windows.
  3. Assuming there is a tmake project file (*.pro) in the current directory (see the Getting Started section below), execute perl tmake-<version>/bin/tmake. This will cause tmake to generate a 'Makefile' for use by GNU make and gcc. If the project file contains a SUBDIRS variable listing dependant subdir projects, tmake will also process any project files located in those subdirs.
  4. On UNIX platforms, simply executing tmake-<version>/bin/tmake is enough, assuming perl is located in /usr/bin/perl. For convienience, add the tmake-<version>/bin directory to your PATH. Other deployment options are to copy tmake-<version>/bin/tmake into /usr/bin and move tmake-<version>/lib to /usr/lib/tmake-<version>
  5. The full list of compilers-platform combinations tmake can generate build files for are listed as sub-directories of tmake-<version>/lib. To have tmake use one of these lib sub-directories, use the -lib command line option or set the TMAKEPATH environment variable to the path of one of these subdirs.
  6. For almost all -lib compiler-platform options, the default output filename is 'Makefile'. The exceptions are -lib unix-kdev, where the output filename will be the projectname.kdev, and -lib win32-msdev where the output filename will be projectname.dsp for app and lib projects, or projectname.dsw for subdir projects. The output filename can be over-ridden using the -o filename command line option, with -o - going to stdout. For a full listing of output types for different build tools (make, nmake, MSVStudio), please see the tmake output manual.
Here are some TMAKEPATH examples:

Unix Bourne shell:

Unix C shell:
    setenv TMAKEPATH /local/tmake/lib/linux-g++
    setenv PATH $PATH:/local/tmake/bin
Microsoft Windows:
    set TMAKEPATH=c:\tmake\lib\win32-msvc
    set PATH=%PATH%;c:\tmake\bin

Supported platforms: AIX, Data General, FreeBSD, HPUX, SGI Irix, Linux, NetBSD, OpenBSD, OSF1/DEC, SCO, Solaris, SunOS, Ultrix, Unixware, Cygwin, MinGW, Mac OS X, and Microsoft Windows. Embedded systems and processors including the arm, ipaq, sharp, cassiopeia, koala, mips, and qnx-rtp.

Supported build tools: make, nmake, Microsoft Visual Studio, KDevelop.

Unix users: tmake requires that perl is in /usr/bin. If your version of perl is elsewehere, either change the first line of tmake or make a small shell script which invokes tmake with the correct perl.

Getting Started

Let's assume you have a small Qt application consisting of one C++ header file and two source files, and that tmake-<version>.tar.gz has been extracted in the current directory: First you need to create a tmake project file, e.g.
  HEADERS   =  hello.h
  SOURCES   =  hello.cpp main.cpp
Then run tmake to create a Makefile:
And finally:
This builds the hello program.

See the generated Makefile for linux-g++.
See the generated Makefile for win32-msvc (Microsoft Visual Studio nmake).

Here are some other invocation examples:

  perl tmake-<version>/bin/tmake -lib tmake-<version>/lib/linux-kcc
  tmake-<version>/bin/tmake -lib tmake-<version>/lib/win32-msvc -o hello.mak
  perl tmake-<version>/bin/tmake -lib win32-msdev CONFIG+=debug

Makefile Templates

The tmake distribution includes three templates and one configuration file for each platform-compiler combination.

  app.t   Creates a build file for compiling applications.
  lib.t   Creates a build file for compiling libraries.
  subdirs.t   Creates a build file for compiling targets in subdirectories.
  tmake.conf   This configuration file contains compiler options, tools, and libraries for a platform-compiler combination.

The project file above does not have a TEMPLATE, CONFIG, or TARGET variable. The default TEMPLATE is app (the .t extension is optional), the default CONFIG is warn_on release, and the TARGET name defaults to the project (*.pro) filename. This project file produces exactly the same result as the above:

  TEMPLATE =  app
  CONFIG   =  warn_on release
  HEADERS  =  hello.h
  SOURCES  =  hello.cpp main.cpp
  TARGET   =  hello

Makefile Configuration

The CONFIG variable specifies what compiler options to use and which extra libraries to link in. These options control the compilation flags:

  release   Compile with optimization enabled, ignored if "debug" is specified.
  debug   Compile with debug options enabled.
  warn_on   The compiler should emit more warnings than normally, ignored if "warn_off" is specified.
  warn_off   The compiler should emit no warnings or as few as possible.

These options defines the application/library type:

  qt   The target is a Qt application/library and requires Qt header files/library.
  opengl   The target requires the OpenGL (or Mesa) headers/libraries.
  thread   The target is a multi-threaded application or library.
  x11   The target is a X11 application or library.
  windows   The target is a Win32 window application (app.t only).
  console   The target is a Win32 console application (app.t only).
  dll   The target is a shared object/DLL.
  staticlib   The target is a static library (lib.t only).

As an example, if the hello application uses both Qt and OpenGL and you want to compile it for debugging, your CONFIG line should read:

  CONFIG = qt opengl debug

The most common tmake options and project variables are described here. See the tmake reference manual for details.

The Application Template

The application template, app.t, lets you compile and link executable programs or shared objects (DLLs). This template recognizes several variables.

  HEADERS   Header files.
  SOURCES   Source files.
  TARGET   Name of executable (adds .exe if on Windows).
  DESTDIR   Where to put the target.
  DEFINES   Tell compiler to define C preprocessor macros (-D option).
  INCLUDEPATH   Sets the include file search path for the compiler (-I option).
  DEPENDPATH   Sets the dependency search path for tmake.
  DEF_FILE   Win32 only: Link with a .def file.
  RC_FILE   Win32 only: Use a .rc file (compile to temporary .res).
  RES_FILE   Win32 only: Link with a .res file.

The Library Template

The library template, lib.t, lets you compile and create static or shared libraries.

The lib.t template supports the same project variables as app.t, but also VERSION. VERSION is the version number of the target library, e.g. 1.40. The version is important for shared libraries.

The Subdirs Template

The subdirs template, subdirs.t, lets you invoke make in subdirectories.

The SUBDIRS variable contains the name of all subdirectories to be processed. If this is the base *.pro file when tmake is invoked, tmake will automatically generate build files for all project files found withing SUBDIRS.

Project File Syntax

The tmake project file has a very simple syntax. You may set project variables, append to project variables, remove from project variable and substitute project variables. To set a project variable:
    HEADERS = gui.h xml.h url.h
If you cannot fit everything on one line, use '\' to split it up:
    HEADERS = gui.h \
	      xml.h \

Project variables contains lists of items (such as header files, compiler options etc.) and use whitespace to separate the items. This means that tmake cannot deal with items containing whitespace. The INCLUDEPATH variable is an exception. If INCLUDEPATH contains one or more semicolons (;), tmake uses the semicolon to separate the include directories, hence you can have include directories containing whitespace (this is quite common on Windows).

Here is an example:

    INCLUDEPATH = C:\Program Files\DBLib\Include;C:\qt\include

tmake supports project variable expension. Use $$ to expand any project variable:


Most often you assign some value to a project variable, but you can also add to, remove from or replace parts of a project variable.

    A   = abc
    X   = xyz
    A  += def			# A = abc def
    X  *= xyz			# X = xyz
    B   = $$A			# B = abc def
    B  -= abc			# B = def
    X  /= s/y/Y/		# X = xYz
The *= operation adds the value if the variable does not already contain it. The /= operation performs regular expression substitution.

You can also set variables from the command line when running the tmake program. For instance, if you want to generate a makefile with debug information:

    tmake "CONFIG+=debug"

Use the unix: or win32: (conditional) qualifier if you want a platform-specific variable:

    SOURCES	   =   common.cpp   # common for all platforms
    unix:SOURCES   +=  unix.cpp	    # additional sources for Unix
    win32:SOURCES  +=  win32.cpp    # additional sources for Windows
    unix:LIBS	   +=  -lm	    # on Unix we need the math lib
If none of the platforms match, tmake looks for the variable in CONFIG variable:
    debug:SOURCES  +=  dbgstuff.cpp # additional source for debugging
Finally, you can set platform and compiler-dependent variables:
    linux-g++:TMAKE_CFLAGS = -fno-rtti

You may define your own project variables to be used by custom templates. A project variable is stored in %project, which is an associative Perl array. Access it like this: $project{"var"} or via the function Project("var"). For example, after reading "", $project{"SOURCES"} contains "hello.cpp main.cpp".

Running tmake

  tmake [options] project files or project settings
  -e[xpr]      Evaluate the Perl expression.  Ignores the template file.
  -n[odepend]  Don't generate dependency information.
  -o[utput]   file    Force output to file. Disables recursion.
  -t[emplate] type    Force using template type {app, lib, subdir}.
  -v[erbose]   Verbose/debugging on.
  -r[ecurse]   Enable recursion even when -o[utput] is specified.
  -l[ib]       Force library to specific platform-compiler.
  -type        Force output to specific type {make, nmake, ...}.
  -unix        Force tmake into Unix mode.
  -win32       Force tmake into Win32 mode.
The -t option overrides any TEMPLATE variable in the project file.

The default project file extension is ".pro". The default template file extension is ".t". If you do not specify these extension tmake will automatically add them for you.

Example of basic use, assuming tmake-<version>/bin has been added to the path:


Example of how to create a Makefile with debugging information:

    tmake "CONFIG+=debug"

Example of how to specify a TMAKEPATH:

    tmake -l /local/tmake/lib/hpux-g++ -o Makefile.mak
Example of how to evaluate a perl expression (print names of headers and source files):
    tmake hello -e 'Expand("HEADERS","SOURCES")'
Note that project settings on the command line must come after the project file, otherwise they will be overridden by the settings in the project file.

The progen Utility

The progen utility creates project files for you. It can be used like this:
  progen -n hello -o
If no .cpp or .h files are specified on the command line, progen searches for .cpp and .h (except moc_*.cpp) in the current directory and below.


  progen [options] [C/C++ header files and source files]
  -lower   Lower-case letters in filenames (useful on Windows).
  -n name  Specify a project name (TARGET).
  -o file  Write output to file instead of stdout.
  -t file  Specify a template file.

Advanced Topics

In most cases you will be happy with using tmake as described above, but sometimes you need to add special compiler options or even add new makefile rules. This chapter describes how to customize your makefiles.

Conditional Project Settings

If you need a special compiler option etc., you can add platform-dependent settings in your project file:
  solaris-cc:TMAKE_CC     = /opt/bin/CC_5.0
  solaris-cc:TMAKE_CFLAGS = -pts
  unix:TMAKE_LIBS         = -lXext
  win32:INCLUDEPATH       = c:\myinclude
  win32-borland:DEFINES   = NO_BOOL
You can prefix a project variable with unix: or win32: to make it specific for either Unix or Windows. You can also prefix a variable with platform-compiler

Your Own Templates

If you know Perl programming, there is virtually no limitation to what you can do with tmake. First you need to know how tmake works.

Template Processing

When you run tmake, it first reads the tmake.conf file. This configuration file has the same syntax as the project file. tmake then reads the project file and sets the project variables it finds, e.g. HEADERS, SOURCES etc. All variables and values are stored in a global associative Perl hash array called project. For example, $project{"SOURCES"} contains "hello.cpp main.cpp" after processing When both the tmake.conf and the project files have been read, tmake starts reading the template file line by line and executes any Perl code it finds in the template.


    #! This is a comment which will be removed.
    This text will appear in the output.
    #$ $text = "The header file(s) are: " . $project{"HEADERS"};
    # This text also appears in the output.
       $a = 12;
       $b = 13;
       $text = $a * $b;
    That's all.
    This text will appear in the output.
    The header file(s) are: hello.h
    # This text also appears in the output.
    That's all.

Using tmake With Lex and Yacc

The standard tmake templates knows how to process C and C++ files, but sometimes you need to process additional files and link them into your project. A typical example is to process lex and yacc files when you're building a parser.

Parser template:

  #! parser.t: This is a custom template for building a parser
  #$ IncludeTemplate("app.t");

  ####### Lex/yacc programs and options

  LEX	    =	flex
  YACC    =	#$ $text = ($is_unix ? "yacc -d" : "byacc -d");

  ####### Lex/yacc files

  LEXIN   =	#$ Expand("LEXINPUT");
  LEXOUT  =	lex.yy.c
  YACCIN  =	#$ Expand("YACCINPUT");
  PARSER  =	#$ Expand("PARSER");

  ####### Process lex/yacc files

          $(LEX) $(LEXIN)

          $(YACC) $(YACCIN)
          #$ $text = ($is_unix ? "-rm -f " : "-del ") . '$(PARSER)';
          #$ $text = ($is_unix ? "-mv " : "-ren ") . '$(YACCOUT) $(PARSER)'; 
The parser template adds some extra rules to the application template in order to build the lex and yacc portions of the project. This template is portable across Unix and Windows since it generates different commands depending on the $is_unix variable.

To learn more about the Expand() function and other Perl functions which tmake provides, consult the reference manual.

Example project file:

  TEMPLATE  = parser.t
  CONFIG    = console release
  LEXINPUT  = lexer.l
  YACCINPUT = grammar.y
  PARSER    = parser.cpp
  SOURCES   = $$PARSER    \
              node.cpp    \
  TARGET    = parser
Here we use macro expansion $$PARSER to avoid writing parser.cpp two places.

Counting the Number of Code Lines

tmake is generic since it is based on Perl. You can create your own templates for other purposes than producing makefiles. Here is an example template that counts the number of code lines in our project.

Template wc.t:

  #! Template that count number of C++ lines.
  The number of C++ code lines for #$ $text=$project_name;
    $files = $project{"HEADERS"} . " " . $project{"SOURCES"};
    $text = `wc -l $files`;
Run it:
  tmake -t wc hello
  The number of C++ code lines for
       25    hello.h
       98    hello.cpp
       38    main.cpp
      161    total
This will only work if the wc program is installed on your system.