C++ Preface: Setting Up Your Environment
In order to get up and running with C++ you will need at least two tools: a compiler and a text editor. First I'll briefly go over the purpose of these tools, then provide information to help you setup your own C++ programming environment.
Every program/application is stored on you computer (or mobile device) in machine language, which is what most people refer to as binary (only zeros and ones). Theoretically you could write an entire program in zeros and ones, but this would be extremely tedious and really easy to make mistakes. Fortunately, programming languages like C++ have been developed that allow us to express long combinations of binary using simple lines of text. The purpose then of a compiler is to convert these lines of text, stored in text files, into a binary program (or app). To edit these text files you'll need some kind of text editor, a program which allows you to open, edit, and save text files. A text editor is similar to a word processor (Microsoft Word, Apple Pages, etc.) but is often much less complicated, as you don't need many of the features that a word processor provides. Finally, to make things simpler many programs have been developed that combine a compiler and a text editor into a single application, often referred to as an integrated development environment, or IDE.
There are many compilers, text editors, and IDEs out there for you to use, and deciding which ones are "best" for you depends on you level of programming experience, the operating system you use, and some personal preferences. Since it would be difficult to provide a comprehensive tutorial for every one of these options, in this preface I'll merely provide lists of free programs of each type that you can use to setup your own C++ programming environment, along with my personal recommendations and tips. After you've chosen your setup you can test it using the text file (or source code) provided below.
Additional Recommendations and Tips
- If you are pretty new to programming, using an IDE will be the fastest way to learn. It is also good practice for other projects, as many other platforms (Apple iOS, Android) also utilize IDEs. Using a compiler and text editor from the command line will take more time to learn, but is in my opinion more rewarding as you get to understand more about how computer programming works.
- When using use an IDE, you may need to download a compiler in addition to the IDE itself. I've tried to mention which IDEs come with a compiler, but make sure you are aware of which compiler the IDE is using; this can often be checked in the settings/preferences of the IDE.
- Some operating systems will come with their own text editor; be sure to check and see if you like the one already provided
- If you have any compile errors when using the source code below (and you've made sure you copied the code exactly!), then try to add a .h to the header file (e.g. <iostream.h>). Some compilers use this notation instead (more on that later).
Integrated Development Environments (IDEs)
- Xcode - This seems to be the go-to IDE for Apple users, and can be used for more than just C++. It can be downloaded from the Apple store. It also automatically includes a C++ compiler (Clang). A nice C++ tutorial using Xcode can be found here.
- Code::Blocks - A simple IDE for Windows and Linux users; a C++ tutorial can be found here.
- Dev-C++ - This IDE is popular for Windows users and comes with a compiler (MinGW).
- C++ Shell - This nifty tool is a text editor and compiler that you can run in your web browser. If you are only testing out C++ or just learning the fundamentals and want something simple, this is it.
Compilers
- GNU Compiler Collection (GCC) - This is one of the most popular free compilers out there for C++, and it is the one I will use for all the following tutorials. For Windows users go to the MinGW website to install the GCC compiler. For Linux consult your specific OS on how to obtain GCC. It seems that Apple is dropping support for GCC, as they use their own compiler, Clang (see below).
- Clang - The main C++ compiler for Apple developers, but also available for many other operating systems. At the website click the Downloads link and look for the Pre-built Binary that corresponds to your operation system.
Text Editors
- GNU Emacs - A simple text editor for UNIX operating systems (e.g. Apple OS, Linux)
- Notepad++ - This is my personal favorite C++ text editor for Windows.
Test Your Environment
To test your C++ environment, either copy and paste the code below into your text editor, or download the file using the link provided below, then open the file in your IDE (if you're using one). Don't worry if you can't understand any of the code yet, we'll cover that in the next lesson.
/* test.cpp
   August 16, 2015
   Steven Dolly
   This program tests to make sure the C++ compiler works. */
// This statement includes the iostream header file into the program.
#include <iostream>
// This statement tells the program to use the standard namespace.
using namespace std;
// This is the main function
int main(){
    // This statement prints "Hello World!" to the screen.
    cout << "Hello World!\n";
    // This statement ends the main function, returning a value of 0.
    return 0;
}
If everything works after you compile the file, you should see the phrase "Hello World!" printed to your computer screen (without the quotation marks) and you've made your first program! In the next lesson, we'll look at the code above and discuss the basic structure and syntax of a C++ program.