Wednesday, January 13, 2010

Compiling C++ from command line

I'm finally taking an introduction to C++ class at college, and I needed to install a compiler. I had intended to use the GCC, but I chose the Microsoft Visual C++ compiler instead because I wanted to become familiar with multiple compilers and because it was dead-simple to install.

Unfortunately, it was not dead-simple to use. After building and running only a simple "Hello, world!" test script, I longed for a simpler way to compile and run my scripts. I found a command-line compiling tutorial in the documentation and tried it out. The procedure worked fine, but it required starting a new terminal through a shortcut in the "Start" menu. I'm allergic to the "Start" menu, so I figured out how to setup the necessary environment variables from the command line. Now, I just run setupcppenv to prepare the environment, and I can compile 'till the cows come home (pardon the idiom).

Requirements

This tutorial is written for people who prefer to use the Windows terminal, so a familiarity with the command prompt is assumed. Also, you'll need to download and install the Microsoft Visual C++ Express Edition compiler; don't worry, it's free.

How to set up Windows to compile C++ from the command line

  1. Create a configuration script

    In order to simplify the configuration process and give us an easy command to remember, we're going to use a batch script to configure the environment for us. VCE comes with a setup script, but it's hidden in the directory in which VCE was installed.

    Find the shortcut "Visual Studio 2008 Command Prompt" in the "Start" menu under "Microsoft Visual C++ 2008 Express Edition > Visual Studio Tools" and view its properties. Create a new file in Notepad or the text editor of your choice, and copy the contents of the "Target" field into the new text file. Save the file as setupcppenv.bat on the desktop and click on it. If you see a command prompt pop up with "Visual Studio 2008 Command Prompt" in the title bar, your new batch script is working.

  2. Modify the batch script

    Unfortunately, the script works a little differently than I would like. To see what I mean, bring up a command prompt and cd to the desktop (or wherever you put the setup script.) Run the script with setupcppenv, then type in exit to close the command prompt. It didn't work, did it. That's because the batch script starts a new cmd process with the variables configured instead of just configuring them in the current process. In order to remedy this behavior, we'll have to modify our batch script.

    Right now, my script looks like this:

    %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86

    Yours will probably look different if you're using a 32 bit Windows version. To prevent the command from starting a new process, we need to take out %comspec% /k and convert the double double quotes into single double quotes. After that change, my script looked like this:

    "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86

    Now, our command prompt will terminate on the first exit.

  3. Set up script bin

    Right now, our setup script works fine, but it only works on the desktop. Obviously, storing all our source files on the desktop is a bad idea, so we need to make our script accessible to the whole computer. We can do that by adding it to the Windows Path.

    Path is an environment variable on Windows systems that lists places for the OS to look for scripts. When you run a command from the command line, Windows looks in all the directories listed in the Path for a script matching the command you entered. If it finds one, it runs it.

    We could, of course, add the script's current location to the Path and use our script anywhere; however, we would have to keep the original script on the desktop forever in order for it to remain accessible. Instead, we will create a dedicated directory for storing command line scripts. We will call it bin and place it in the home directory. You can find your home directory by running echo %homepath% from the command prompt. Create a directory called bin in there, and move setupcppenv.bat into the new directory.

    Now we will add bin to the Path. Open the Control Panel, and find the "System" options (you might have to switch to Classic View in Vista). Open the "Advanced system settings" dialog box and click on "Environment Variables". Edit Path under "User variables". Path is a list of directories separated by semicolons; add your new bin directory to it by appending a semicolon and the new directory's full path (C:\User\Sage\bin for me) to the Path. Don't add any spaces between the semicolon and the paths. If the field is empty, put in just your bin path. For example, the Path C:\Program Files\Acme Corp;C:\Program Files\Omnisoftware would become C:\Program Files\Acme Corp;C:\Program Files\Omnisoftware;C:\User\Your user name\bin. Save your change by hitting "OK" in all the dialog boxes.

  4. Test everything

    Now, we need to make sure everything has come together properly. Start by creating a new file in your text editor and plugging in this code:

    
    #include <iostream>
    
    int main() {
    
        std::cout << "Everything is working properly!\n";
        return 0;
    }
    

    Save the file as test.cpp on the desktop, then open a command prompt. Set up the environment with setupcppenv, cd into the desktop, and compile your C++ test script with cl /EHsc test.cpp. Run test.exe and revel in the simplicity of your new build procedure.

Now that I'm learning C++, you may start seeing some C++ on this blog. Don't be alarmed; I'm not turning into a pointer-crazed, multiple-inheriting C++ coder, thanks to my strong Python foundation. I plan to stick to the somewhat-comprehensible features of C++.