In this course we will be using the GNU toolchain for building our C/C++ programs. A toolchain is a group of programs that is used to develop and test software. If you really want to use a different toolchain (e.g. Microsoft VisualStudio or XCode), I’m not going to stop you. However, the only toolchain officially supported is going to be GNU. If you choose to use something else, I may or may not be able to help you figure out why your code isn’t building but I can make no guarantees.
There are a few reasons for using the GNU toolchain as opposed to MSVC, Xcode, etc:
The GNU toolchain is free and runs on all major operating systems including MacOS, Windows, and Linux. This means that you are free to install it on your personal computer or anywhere else and you aren’t restricted to working on a computer that has a license. It also means that everyone in the course is using the same tools and code that builds on one machine should build on others.
Using a toolchain like GNU exposes certain parts of the compiling process that others, particularly IDE’s like Microsoft Visual Studio and Xcode, hide. In my experience, one of the biggest issues people have with getting their code to build is not understanding the build process.
Possibly the most important reason is that if you ever find yourself writing software to run on a cluster or compute server, it will most likely be running some form of Unix. While the exact compilers available may not be the GNU compilers they will be similar and you will need to know how to build software in that environment.
What follows are instructions for getting the GNU toolchain installed and working on Windows, MacOS, and Linux. Once you have the GNU toolchain installed, go ahead and try to build the Hello World program to try out your newly-installed compiler.
To write software in Windows you will need three things: A text editor, the GNU compiler toolchain, and enough of a Unix system for the GNU build tools to work. For the latter two, we will be using a system called MinGW.
An important part of any programming setup is a decent text editor. There are a number of good ones for windows. Most of the lab and office computers have WinEdt installed. Another good text editor for windows is Notepad++. The two most important things about your text editor is that it has syntax highlighting and decent handling of indentation. If you’re not sure what I mean, just install Notepad++ and you’ll figure it out soon enough.
MinGW or “Minimalist GNU for Windows” is a project that provides the GNU toolchain and just enough other standard Unix tools to be able to build software. Because the GNU compiler toolchain is designed to work in a Unix environment and windows isn’t Unix-based, you need just a little more than just the compilers. Fortunately, the MinGW project has all of this bundled into a easy-to-use installer. To install MinGW, do the following:
Download the latest version of the mingw-get-inst installer.
Run the installer
When it asks whether you want to use the pre-packaged repository catalogs or download the latest, go ahead and tell it “Download latest repository catalog”.
When it asks you where to install, tell it to install in
C:\MinGW\
. You can tell it to install somewhere else, but
you have to be careful. The reason is that some of the MinGW tools have
problems with files and folders that have spaces in their names. So long
as you avoid folders like “Program Files” and “My Documents”, you should
be fine.
When it asks you to select components, you need at least:
Go ahead and tell it to install.
Once MinGW has installed, you will have an entry for MinGW in your Start Menu. Under this is a program called “MinGW Shell.” This provides you a basic Unix shell from which you can run compilers and other things.
Many Mac users don’t know this, but MacOS is really a Unix system at heart. While it has a nice fancy Apple-branded user interface, it’s running Berkeley UNIX under the hood. This makes it very nice to work with once we coax it into installing the GNU toolchain. Again, this isn’t that hard:
First, you need to register yourself as an apple developer here. As you go through the registration process you will see several different types of paid developer accounts. There is a free one, you just have to dig for it.
Use either the Mac App Store or your MacOS CD (depending on your MacOS version) to install Xcode.
In Xcode, go to preferences then “Downloads”. From there you can install the “command line tools”
You may want to add “Terminal” to your doc as you’ll be using that a lot.
An important part of any programming setup is a decent text editor. There are a lot of good text editors out there. One option is to use the text editor built into Xcode. Even though we will be using the command line for compiling our programs, you can still use the text editor in Xcode. Another option is TextWrangler which is a nice, basic, programmer’s text editor.
Linux is based on the GNU toolchain, so installing it is very simple if it isn’t installed already. Simply use your package manager to install gcc, g++, and make. You will also need to install a decent text editor like Vim, gedit, or Kate.