A compiler is a program that translates one language (high level) into another language (e.g., assembly language or machine specific language). A compiler translates source code (plain text) into object code (normally in a form suitable for processing by other programs (like a linker)). The most common reason for wanting to translate source code is to create an executable program.
After the compiler translates the source code in object code, the object(‘s) have to be linked into an executable. This is done by a program called a linker (in most cases the compile stage and link stage are done automatically. It is also possible to do these stages separately).
There are many different compilers available. Some compilers are operating system specific other can be used on different platforms. In the following two sections we take a look at two compilers: GNU compiler and Visual Studio.
The GNU Compiler Collection (GCC) includes front ends for C, C++ , Objective-C, etc., as well as libraries for these languages. GCC can be used on many different platforms, but is commonly used on Unix and Linux.
If you want to use GCC on a windows machine you should take a look at Cygwin. It is also possible to install a Linux partition besides a Windows partition (multi boot system). We recommend you use Ubuntu distribution for this.
You can use your favorite text editor to write your programs in. After writing your program you can use the following command to compile the program:
For c programs:
For c++ programs:
On some systems, g++ is also installed with the name c++.
It is also possible to compile C++ programs with gcc, however the use of gcc does not add the C++ library.
g++ is a program that calls GCC
and treats .c, .h and .i files as C++ source files instead of C source files
unless -x is used, and automatically specifies linking against the C++ library.
Visual Studio 2005/2008 (Express edition)
Visual studio is the developer studio from Microsoft. It provides a complete set of development tools to create windows programs in many different languages (like visual basic, visual c++, etc.). The complete developer studio is not free. But it is possible to download an express edition of Visual Studio C++ 2005 or 2008. (You have to register).
If you want to use Visual Studio Express for compiling win32 (console) applications you have to install the platform SDK as well. A complete instruction on how to install can be found here. Note: the tutorials on this site are win32 console applications.
After you installed Visual Studio C++ and the platform SDK you can start your project.
To set-up a win32 console application do the following:
- File, New, Project
- Project types : Visual C++, win32
- Templates : Win32 Console Application
- Give the project a name and press OK and then click next.
- Check Console Application and Empty project by Application settings.
- Click finish.
An empty project is now made. To add a new source file do the following:
- In the solution explorer select Sources files and right click on it.
- Add, New item, Templates: C++ file (.cpp), Name the file and press add.
- File, Save all.
The last thing to do is to set some Project properties:
- Project, Properties….
- Select General
- Set Character from “Use Unicode Character Set” to “Use Multi-Byte Character Set”.
- Exit with OK.
You are now ready to program your first program. After writing your program you can compile it by pressing F7.
The examples included in the C and C++ tutorials are all console programs. That means they use text to communicate. All compilers support the compilation of console programs. Check the user’s manual of your compiler for more info on how to compile them. (It is not doable for us to write this down for every compiler).