 |
Department of Engineering |
 |
 |
Installing C++ compilers
To develop C++ programs on your own machine you need to ensure that you have a text editor and a C++ compiler. Soon you'll also need the GLUT graphics libraries too.
Here are some options that have worked for us in the past. Included are
instructions on using some libraries too. Note: if you're installing so that
you can do the Mars Lander exercise, see the Mars Lander page in CamTools from which the information here has been adapted.
There are two ways you can go about assembling these tools on Windows. You
could install Microsoft's Visual
C++ compiler or Bloodshed both of which have a built-in text editor. You would then just need to install Octave and the GLUT graphics libraries to be ready for work after the 1st year. The other option is to install Cygwin. This essentially gives you a version of the Linux operating system that runs on your Windows desktop. Linux provides everything you need, including a choice of text editors, a C++ compiler, Octave and the GLUT graphics libraries.
- The Visual C++ route -
This is the way to go if you don't want to experiment with the Linux way of doing things, but would prefer to stick with the familiar Windows look and feel. You will need to install several components as follows:
- First, install Microsoft's Visual C++ 2010 Express Edition by following the
instructions: here.
- Next (for the Mars Lander) download glut-3.7.6-bin.zip from here. Unzip the archive and
copy three files to your hard disk as follows:
- copy glut32.dll to C:\Windows\System32 (for 32-bit Windows systems) or C:\Windows\SysWOW64 (for 64-bit Windows systems, which is what you have if this folder exists)
- copy glut.h to C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl (for 32-bit Windows systems) or C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\gl (for 64-bit Windows systems, which is what you have if this folder exists)
- copy glut32.lib to C:\Program Files\Microsoft
SDKs\Windows\v7.0A\Lib (for 32-bit Windows systems) or C:\Program
Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib (for 64-bit Windows
systems, which is what you have if this folder exists)
- Finally (for the Mars Lander), install the latest stable release of Octave by following
the instructions here. The
installer will ask you a number of questions: in each case, the default answer
is fine. You might also want to install the SciTE text editor for writing Octave .m files. Alternatively, you could use one of the default Windows text editors, but beware their tendency to save text files with the extension .txt, you will need to change this to .m for Octave programs.
- The Cygwin route -
If you decide to go down the Cygwin route, just follow the installation instructions on the Cygwin website (if you get presented with an empty list of download mirror locations, disable your firewall and start again). During the installation process, you get to choose from a large number of optional packages. In addition to the defaults, you must select:
- gcc-g++ and make under the Devel tab. These are the C++ compiler and a utility to make compilation more straightforward.
- opengl under the Graphics tab. This provides the necessary OpenGL/GLUT graphics libraries and headers. Do not install the freeglut package.
- octave under the Math tab. (useful after the 1st year)
- xinit and font-adobe-dpi75 under the X11 tab.
- A text editor (or two!) of your choice from under the Editors tab. Good choices would be nedit or emacs-X11: the latter is the text editor you most probably used in the DPO when doing the computing laboratories.
To select these packages, just click on the circular-arrow icon next to the package name. Note that dependent packages are automatically selected for you: for example, when you select emacs-X11, emacs is added in too.
The installation should take ten minutes or so. When finished, the installer sometimes queries whether the installation was successful: tell it that it was! The installer helpfully creates a shortcut on your desktop, unless you explicitly asked it not to. Do not use this shortcut to start Cygwin! Instead, you should start Cygwin from the main Windows applications menu by selecting Cygwin-X->XWin Server. This enables windows and graphics, which you will need to run the Mars Lander application. A new X icon should appear in the system tray, and you should also now see a Cygwin shell in a new window (if not, right click on the X icon and select Applications->xterm).
A shell is a program that lets you control your computer (run programs, examine the contents of directories, etc) from the keyboard. It typically gives you a prompt, like $, and you can type commands into it. When a program or command has finished, you get the $ prompt back. It would serve you well to learn a few things about the operation of the shell (it's exactly the same as the command line interface to the DPO Linux machines). A good place to start is the first few sections of external link: this excellent tutorial. For example, to launch the editor in such a way that you get the $ prompt back in the shell straight away and are able to issue new commands, type nedit & or emacs &.
Here's a quick guide to getting started
- Type xterm & in the shell. Type emacs test1.cpp & (or use nedit if you prefer). You can now edit the C++ source code in your text editor.
- Type g++ -o test1 test1.cpp to compile your program.
- Type ./test1 to run your program. If you want to interrupt the program type ctrl-c in the shell window.
- When you have finished with Cygwin, type exit in all the shell windows. Then right click on the X icon and select Exit
- The Bloodshed route -
The installation of Bloodshed takes a handful of clicks, and the integrated IDE
(using the MinGW compiler, which is part of the package) looks suitable.
You need to ensure that you have a text editor and a C++ compiler.
The first of these is no problem: Mac OS X comes with a perfectly adequate editor called TextEdit, though you might prefer to install Aquamacs, which is a pretty version of the Emacs text editor you have been using in the DPO. Alternatively, you could use the built-in editor in the Xcode integrated development environment (see below).
For the C++ compiler, you will need to install the Xcode development tools from your installation CD (the latest Xcode release is also available on Apple's website, but beware, it is not compatible with older versions of Mac OS X). The installer will ask you a number of questions, the default answer is always fine. There are two ways you can proceed to use the compiler: by typing instructions into a terminal, or from within the Xcode integrated development environment. Both of these options are described in more detail below.
The integrated development environment option
This is the way to go if you don't want to experiment with the Linux way of doing things, but would prefer to stick with the familiar Apple look and feel. In Finder, start Xcode by double-clicking on Devices->Macintosh HD->Developer->Applications->Xcode. The main Xcode menu should appear at the top of the desktop. From the File menu, select New Project.
If your Xcode window doesn't have an iPhone OS option, then
- Select Command Line Utility then C++ Tool, then press Next. In the Project Name box, type test1 (or whatever program name you prefer) then press Finish. The main project window will now appear.
If you don't have a Command Line Utility option, but you do have a Command Line Tool option, select that with type C++ stdc++, then press Choose, and in the Save As box choose a program name.
- Click on the main.cpp file, edit and save it. The Build and Run menus contain further options for compiling, debugging and running. The Console option under the Run menu is particularly useful, since it displays a console in which you will see the output of the cout commands.
If your Xcode (version 3.2.1 or higher) window does has an iPhone OS option, then try the following.
- Select MacOS (rather than iPhone OS) and Application, then click on the Command Line Utility icon and select C++ stdc++. Press the Choose button. In the Save As box type test1 (or whatever program name you prefer) then press Save.
- Left-click on main.cpp (which by default has the code to print "Hello World") and write your code.
- The Build and Run menus contain further options for compiling, debugging and running. The Console option under the Run menu is particularly useful, since it displays a console in which you will see the output of the cout commands.
(info by ahg) There's a bug in recent Xcode releases which affects stringstreams. To apply a workaround - under the Project menu, select Edit Active Target for your program then the Build option. Scroll down, and you should find a line that says Preprocessor Macros _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1. Delete _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1..
The terminal option
Your Mac runs a version of the Unix operating system (very similar to Linux) with a proprietary front-end desktop. If you want, you can use it just like any other Unix system, by typing commands directly into a terminal. By doing so, you will learn what goes on "behind the scenes" whenever you press a button on some application's shiny graphical user interface!
Start a terminal from Finder's Applications folder (you will find Terminal under the Utilities sub-folder). The terminal runs a standard Unix shell, which is a program that lets you control your computer (run programs, examine the contents of directories, etc) from the keyboard. It typically gives you a prompt, like >, and you can type commands into it. When a program or command has finished, you get the > prompt back.
It would serve you well to learn a few things about the operation of the shell (it's exactly the same as the command line interface to the DPO Linux machines). A good place to start is the first few sections of this excellent tutorial. For example, to launch the editor in such a way that you get the > prompt back in the shell straight away and are able to issue new commands, type aquamacs &.
Here's a quick guide to getting started
- Use the cd command to navigate to a folder where you want to work
- Start Aquamacs (or TextEdit if you prefer) from Finder's Applications menu, and write your program. Save it as test1.cpp (or whatever program name you prefer) in the folder where you want to work
- Type g++ -o test1 test1.cpp to compile the code
- Type ./test1 to run the program.
- When you have finished with the shell, type exit.
The cygwin installation instruction work on vista
g++ (a C++ compiler) and emacs (a text editor) are usually available from the command line. To compile a simple program called testing.cc you can do
emacs testing.cc &
to start an editor. Write the code, save it, and type
g++ -o testing testing.cc
./testing
to compile then run your program.
Friendlier "Integrated Development Environments" (IDEs) like Anjuta might also be installed by default. You can download geany (the IDE we use in the DPO) for SuSE Linux from the openSUSE_11.0/x86_64 repository.
To compile GLUT graphics programs from the command line try something like
g++ -O3 -o myprog myprog.cpp -lglut