Creating and Compiling Basic C++ Programs

Basic Environment

Our basic environment is the command line.  I suggest the Powershell although the regular command shell will work too.  See Setting up the Environment in our Scholar site for more information.  Our basic command is cl, which stands for compile.  You will then need to list the file(s) you want to compile

I suggest creating a folder for your c++ projects.  You can do this in your Windows Explorer or on the command line with mkdir <directory name>.  mkdir stands for Make Directory.  To move or change your current directory, use cd for change directory.  If you press the Tab key after you've starting typing a directory name, it will autocomplete for you, if there is a directory that matches.

So a quick rundown of some basic commands for your use are:

  1. cd cd means to change directory. You need to include the name of the directory you want to move into. The directory must be in your current directory. You can move down several layers deep as long as you have the complete path from your current directory to the one you want and you separate them with a . In windows if there is a space in the directory name, add quotes, ", around the path, e.g. cd "C:\Users\Dave\My Documents"
  2. dir dir means to give a list of the current directoy's contents.
  3. mkdir mkdir will create a directory with the given name, e.g. mkdir c++ will make a directory in the current directory with the name c++

Compiling a Sample Program

After you have created your directory for your projects I would make one directory per project.  This will keep your code separate and easier to find.  So cd into the directory where you would like to create a project.  Using an editor like Notepad++ enter the following lines of c++ code.

#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}

Save this file into your current working directory with a name like main.cpp.  Now on the command line, you can enter:
cl main.cpp
This should produce both a main.obj and a main.exe.  Typing .\main.exe will run your program.  You need to add the .\ before the name of the program because the command line doesn't know where to find main.exe.  .\ tells the command line to look in the current director for the main.exe program.