C Tutorial – Compilers (GNU and Visual Studio)

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.

GNU Compiler

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 favourite text editor to write your programs in. After writing your program you can use the following command to compile the program:

For c programs:

# gcc -o <output name> <your-source.c>

For c++ programs:

# g++ <your-source.cpp> -o <output name>

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. Note: most of 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 (in Visual Studio 2005) 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.

Important

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).

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 7 responses to “C Tutorial – Compilers (GNU and Visual Studio)”

Why not let us know what you think by adding your own comment!

  1. hiteshgiri goswami on September 10th, 2011:

    i like your easy language and good and simple discription thank u

  2. sumit on October 29th, 2011:

    U explained the C very basics very well ,thanks for that but besides i wanna know can we print the compilation or build date every time we build the code ,if its yes than how?(I m using Visual Studio)

  3. chintamani on August 17th, 2012:

    hey..u explained very well.
    thanks for such important knowledge

  4. sriveni on February 7th, 2013:

    I want simple stack progranmming and explanation

  5. harishma on july 18th,2013 on June 18th, 2013:

    i’m a not a newbie to compiler………whats i already known,thats only here also given….anyway explained gud and nice..
    tnx for sharing………

  6. vitalreddy on July 24th, 2013:

    i searched so many websites for c c++ run in visual studio ..i didn’t find anything … just today i saw this and try ed it works…thanks a lot to you…………..

  7. MIlo on September 12th, 2013:

    If you want to create a C/C++ program without the Visual Studio IDE you can download the DEV C++ IDE. It is simple and not full of things.