Monday, March 24, 2008

Building Visual C++ Apps on the Command Line

I recently started spending some time working on my laptop; in coffee shops, libraries, etc. Much of my work involves working in C++, which means that I've gotta spend time compiling. Unfortunately, my laptop is old. Running Visual C++ on this machine, which I need for much of my work, is like putting it on a treadmill turned up to high. It's ok for a while, but gets tiring awfully fast. Visual C++ on my desktop machine isn't a problem. It's got gobs of memory and a nice dual core CPU. A few days away from it has made me realize just how spoiled I've become by it.

Fortunately, Visual C++ can do builds on the command line. Editing can be done in a less memory intensive application, which in my case, means that my laptop won't have to use the hard disk when it runs out of memory, something that happens all too quick these days.

Here's how to do it, or rather, here's at least one way to do it:

First, a command prompt needs to be set up for use with the Visual C++ toolset. There are two ways to get this running:

  1. Launch a command prompt and run the batch file, vcvarsall.bat. On my laptop, this is installed to C:\Program Files\Microsoft Visual Studio 8\VC\. I installed Visual C++ 2005 to the default location. If you've got this installed, it's probably in there too.
  2. Open the "Visual Studio 2005 Command Prompt" shortcut that the Visual C++ 2005 installer creates. It's in the, "Visual Studio Tools" folder.

Once you've got a command prompt window open, CD into the directory where your .sln file exists. From there, to build all configurations (Debug, Release etc.), type in the following command:

vcbuild TheApp.sln

If you'd like to build just one project configuration, perhaps "Debug", then type in the following:

vcbuild TheApp.sln Debug

That's it! Nice and simple, no IDE required. Of course, you'll lose out on things like a debugger, or the ability to point and click your way to build management, however there are ways to do those outside of the IDE. In my case, I figure I can wait to do those when I'm at my nice and fast desktop machine, or I can just run the full Visual Studio IDE on my laptop every once in a while.

Update: More information can be found on VCBUILD here:

Update #2: Turns out VCBUILD has a problem regarding dependencies. If one use Visual Studio's Configuration Manager to set up a linker dependency for a C++ project, VCBUILD won't use that information. It'll build the projects, but will not build the projects in the correct order, nor will it pass the correct information to the linker . One workaround is to use DEVENV.EXE, which serves both as the exe for the full IDE, and as a way to build projects at a command prompt. It's a bit different than VCBUILD, and I didn't notice any way to build all configurations in a .sln, but it can be utilized to build individual ones. For example, to build the configuration, "Debug", the following can be used:

devenv.exe TheApp.sln /Build Debug

To note, this will not launch the full IDE; it will build the app, use the current console window for logging, and then exit, just like VCBUILD. No GUI will be shown.