John Lam 1997 — 2026

Building C++ projects with NAnt

Now that I’ve more-or-less figured out how to build C# projects using NAnt, I’m turning to the real meat of my development process: compiling C++ projects. In theory, it should be no different from using the csc task in NAnt. The reality, however, is that the C++ compiler has a lot more knobs and switches that you can and should flip while doing development.

There are two unique challenges in creating a NAnt build file that works correctly with C++ projects. The first is making sure that all of your favorite CL.EXE command line options are set correctly. However, if you’re like me and were raised in IDE’s, this can be a really daunting task. However, the VS.NET IDE has a savior: the build log. If you look at the build log, you’ll see the actual command lines that were used by VS.NET to compile your application. This makes it considerably easier to replicate your VS.NET build inside of a NAnt build file.

The second is doing all of those little things that we take for granted in ATL projects. Things like running the MIDL compiler or registering your COM DLL’s using regsvr32.exe. Fortunately, these commands are captured using the VS.NET build log as well, so it’s mostly a matter of patience and typing to get these things integrated into your NAnt build file.

To this end, I’m doing the NAnt equivalent of “Hello, World” for an ATL project in VS.NET. This is a simple AppWizard generated ATL skeleton project.

WATCH OUT FOR DOUBLE QUOTES! Most command line options contain double quoted filenames. Most people like to use double quotes to delimit their XML attribute values (I know I do). This is a very, very bad mix in NAnt build files. Make sure that you use single quotes to delimit your attribute values so that you can continue to use double quotes in your command line options. If you change your command line options to use single quotes, you will get tons of unexplainable errors in your build scripts.

If you want to take a peek at this work in progress, click here. There’s still a bunch of things that I need to change in this build script. Most notably, none of my fun versioning / VSS stuff lives in this file. But I managed to get a simple script that compiles a debug build of an ATL project up and running. After working with this script a bit, I realize that there’s a few additional features that would make NAnt better:

  1. A way of defining symbols in something that resembles a NAnt fileset. Defining them on the command line is really awkward.
  2. Additional tasks for running midl.exe and rc.exe that does some dependency checking.

These are fairly low-priority items right now since the exec task provides a reasonable, albeit sub-optimal workaround.

One other thing that I never got working was the /Yc and /Yu options for controlling the generation / use of precompiled headers. If you look closely at the build script, you’ll notice that I used the /YX option to let cl.exe figure out what to do about precompiled headers. I’d be interested in hearing whether /Yc + /Yu is preferable to using /YX. Update: I’ve posted a new entry that shows how this problem was solved.

Next up is to make these scripts work on release and debug builds to capture the most commonly used options.

1 archived reader comment

sushil kumar patel (2003-08-28)

i want to join you