Setting up to compile C++ on Windows
How you set up to compile C++ programs depends on whether you are using Windows
or Linux/Unix. This page describes the approach to use if you have a Windows
machine. If you have a Linux/Unix machine, read
this page
instead.
There are many free C++ compilers that can be used with Windows. I am most
familiar with Cygwin, available at
http://cygwin.com.
These instructions assume you plan to downloaded this compiler, but it applies
to
many other compilers with small detail changes.
Download Cygwin, which is a self-extracting compressed file. Execute the file,
follow the provided instructions, and the program will install itself.
Once you have installed the program, you should be able to execute it from a
DOS window. Open a DOS window and type g++ -v to establish that the
program has been installed and can be executed.
When I compile I use a batch file named gccp.bat with this content:
@echo off
echo compiling C++ using -ansi -pedantic-errors -Wall
g++ -ansi -pedantic-errors -Wall %1 %2 %3
If you want this batch file, simply copy it from this page and save it on your
system in a file named gccp.bat. The file shold be located in a directory that
is part of your path satement.
This batch file sets up the most strict ANSI standard compliance level, making
it necessary for the programmer to pay attention to many compatibility and
style issues. There are even some incompatibilities within Cygwin's own library
routines, so this batch file may not always result in a successful compile even
if your program is flawless. But in general, it is a good idea to establish high
standards as a student, to become accustomed to good programming style.
If the Cygwin compiler is executed directly, without using the batch file, its
behavior is more relaxed. This may sometimes be necessary.
Set up a convenient data directory in which to place your programs. Create a
file named temp.cpp with this content:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Again, you should be able to simply copy this little program from this page,
and use a text editor to save it as temp.cpp.
Open a DOS window, change to the directory in which temp.cpp is located, and
type this:
gccp temp.cpp
If you have not made any errors, the program will compile uneventfully, and a
program file named a.exe will be created in the same directory.
Type a and press Enter. The program should run, and print
Hello world! on the display.
This series of actions confirms that you have acquired a C++ compiler and it is
working properly.
Arachnophilia
It will be more convenient and productive for you to acquire and install a
programming editor to use during this tutorial. I recommend my free program
Arachnophilia.
Arachnophilia can beautify your code, that is,
indent the program code in a classical way, and it can launch the compiler
automatically at a keystroke.
If you decide to use Arachnophilia, here are some specific notes. You will need
to acquire the latest version of Arachnophilia (Version 4.0, build 5237 or
newer). To beautify a C++ program, simply press Ctrl+H. When the Arachnophilia
beautifier scans your program, if it finds unmatched braces, brackets or
parentheses, the program will warn you, providing an easy way to detect errors
without launching the compiler.
To launch the compiler from within Arachnophilia, simply choose the keyboard
macro editor (Tools
Toolbars/Macros/Edit Keyboard Macros, or use the
provided toolbar button). Choose a letter for your macro (let's say you have
chosen C), then type this:
[Save][SystemComKeep:gccp [arach_filename]]
Press Apply and the macro becomes available (and is saved by
Arachnophilia for future use). Load your C++ program into Arachnophilia. While
working, you may save and compile your program at any time by pressing Alt+C.
On execution of this command, Arachnophilia will save your program, then open a
DOS window and launch the compiler with the document path and filename as its
argument. At the end of the compile, the DOS window will stay open and you may
review any compile errors, or you may run the program by typing a.
Each time you execute the Arachnophilia C++ compiler macro, a new DOS window
is opened, so you may want to review the window's contents, then close the
window before editing your program further. There are other arrangements you
can set up as you become more familiar with Arachnophilia.