]
Cppmake is a front-end to make that builds C++ programs
with
less effort and more accuracy than writing makefiles manually. It was developed by
Bitwiser Labs as a build tool for
Netwiser. Building a C++
program with cppmake is similar to compiling a Java program.
The idea is that you specify a classpath and cppmake will
automatically find all the classes needed by your program. Once
found, all of the classes are compiled and linked into an
executable or library. It's similar to
makedepend,
but more powerful. The difference between cppmake and makedepend
is that cppmake infers source dependencies from the header dependencies
it discovers and recursively finds all the source dependencies required
to build. It then generates a complete makefile
for the build (without any editing on your part) and actually builds
the executable or library by invoking make.
The benefits of using cppmake to build C++ programs include
- You don't have to write a makefile when you start a new project
- You don't have to maintain a makefile as your project evolves
- You don't have to write a makefile ever!!!
- Header file dependencies are automatically kept up to date
- You can switch from one implementation of a group of classes to another on the fly by simply changing the classpath
Let's begin with an example. Suppose you want to build a program
called joe and the main function is in joe.cpp.
Your programs classes live in the current directory,
../../common, and /usr/src/base. The only thing you need to do is set the classpath
(just like a java program) to "
.:../../common:/usr/src/base" (unix/cygwin) or "
.;..\..\common;C:\usr\src\base"
(windows). This tells cppmake to first search for classes in the
current directory, then in ../../common and finally in /usr/src/base. You simply run cppmake on joe.cpp to build the
executable.
$ cppmake --classpath ".:../../common:/usr/src/base" joe.cpp
The result of this command is an executable called joe.
Cppmake figures out which classes on the classpath need to
be included in the executable and defers the work of actually compiling
and linking the executable to the make command. Therefore the
next time you run cppmake, only files that need to be recompiled are
recompiled.
Now let's do a more complicated example. Suppose you want to
build the same program as in the previous example, but this time you
want more control over the build. You want to explicitly name the
executable joe.exe, you want the resulting files to go in a directory called
build, you want to specify the compiler as g++, you want to pass the
-Wall -g compiler flags, and you want to link with the pthread library.
$ cppmake --classpath ".:../../common:/usr/src/base" -o joe.exe -d build -x g++ -f "-Wall -g" -l "-lpthread" joe.cpp
All of these options can be given through environment variables as
well as the command line. The following are equivalent to the
previous example.
| Bash on Linux or Cygwin |
Windows with Microsoft NMAKE instead of make |
$ export CLASSPATH=.:../../common:/usr/src/base
$ export OUTPUT=joe.exe
$ export DIRECTORY=build
$ export CXX=g++
$ export CXXFLAGS="-Wall -g"
$ export LDFLAGS=-lpthread
$ export CPPFILES=joe.cpp
$ cppmake |
> set CLASSPATH=.;..\..\common;C:\usr\src\base
> set OUTPUT=joe.exe
> set DIRECTORY=build
> set CXX=g++
> set CXXFLAGS=-Wall -g
> set LDFLAGS=-lpthread
> set CPPFILES=joe.cpp
> set MAKE=NMAKE
> cppmake |
When an option is given by an environment variable and on the command
line, the command line option takes precedence. One
exception to this rule is that C++ source files can be given with the CPPFILES
environment variable and the command line at the same time.
Cppmake can also be used to build libraries. The difference
between building a library and an executable is that when building an
executable you only want classes that are needed by the
executable, but when you build a library you want all the C++ files on
the classpath. The --library option tells cppmake to include
every C++ file found on the classpath in the build. You also have
to pass your compiler specific linker options to build a library,
cppmake will not know to give those options automatically because
they are compiler specific. The following example creates a
library on Linux assuming CLASSPATH has already been set as in the
previous example.
$ cppmake --library -o libjoe.so.1.1.1 -l"-shared -Wl-soname,libjoe.so.1"
The --clean option tells cppmake to remove all the object files and the
output. The --help option prints a summary of all the options and
exits.
Cppmake is written in python.
You need python 2.3 or greater to use it. Linux and Cygwin
come with python and can run cppmake with no problem. For regular
non-Cygwin Windows users you can either install python for Windows or
use the Windows executable provided below which does not require python.
Cppmake uses the make command, so you need to have a make command in
your environment. Currently cppmake has been tested with Gnu Make
and Microsoft NMAKE.
Finally cppmake requires that you use a few common sense programming conventions that most C++ programmers already use anyway.
- Associated source files and headers should have the same base
name. For example if you declare a class in Bob.h then the method
implementations should be in a file called Bob.cpp and in the same
directory as Bob.h. If for some reason a source file and
header can't have the same base name you must list that source file in
CPPFILES or on the command line, cppmake will not be able to find it
automatically.
- When including headers use the file name relative to the classpath directory it lives in. For example if /usr/src/base is on the classpath and you want to include /usr/src/base/bob/Bob.h, then your include statement should be #include "bob/Bob.h".
- Source files must have a normal C++ file extension such as .cpp, .cxx, .cc, .c++, .C or .c.
- Header files must have a normal C++ file extension such as .h or .hpp.
To use the pure python version just save this file in a
directory on your PATH such as /usr/bin and make it executable. Linux
and Cygwin users should use this. You can only use the pure
python version on Windows if you have python installed.
cppmake 0.5
To use the native Windows executable just download this file and place it in your C:\WINDOWS folder.
cppmake.exe 0.5
The Windows executable of cppmake was made with
py2exe.
Since Windows does not come with a make program you will need to
install one yourself. One way to do that is to install
Cygwin and use
Gnu Make that comes with it. Another way is to download
Microsoft NMAKE.
Cppmake is © Bitwiser Labs, 2006. You can use cppmake under the conditions of the
GPL.
If you have questions, comments, suggestions or find bugs send email to
boder@bitwiserlabs.com.
Limitations
- Directories on the CLASSPATH can not have spaces in their names.