-->
C / C++ - Compiling In UNIX

C / C++ - Compiling In UNIX

Introduction

The programming language C and its close relative C++ are very popular programming languages today. The language C was originally at Bell laboratories in the earlier 70s the aim of C's original design was to blend the power and control of a language that is .close to the hardware. while still facilitating higher level machine independent programming. [1]   Bjarne Stroustrup (Professor and holder of the College of Engineering Chair in Computer Science at Texas A&M University!) developed the C++ programming language to provide stronger type checking and support a "wider range of programming styles" such as object oriented programming. C++ is commonly referred to as "a better C" in that most C programs will compile as C++ programs but C++ allows you extra freedoms and more tools to use in your programming. [2]

Here at the Computer Science department we use the "GNU Compiler Collection" (commonly referred to just as GCC) which is a widely popular open source compiler. As of Fall 2003 the current version being used at the department is 3.3. For compileing C code use the gcc command, when compiling C++ code use the g++ command.

A bit about file extensions

In all the examples below I use .c or .cpp extensions; however GCC will recognize a wide Varity of C or C++ file extensions. You may use the following with GCC:

Also in the later steps an individual file may be compiled to object code which will have .o extension.

To compile a C or C++ program

  1. Open a Unix Session
  2. Go to where your source code is, you can use Emacs, Pico, or vi to edit your code.
    Below are two sample HelloWorld programs in C and C++:
    #include <stdio.h>


    int main(int argc, char* argv[]) {
        printf("Hello World in plan C\n");
    }
          #include <iostream>
    using namespace std;

    int main(int argc, char* argv[]) {
        cout << "Hello World with C++ streams!" << endl;
    }

  3. To compile your C source code:

    > gcc <your source file>.c -o <your executable binary file>

         Or use the following for C++ code:

    > g++ <your source file>.cpp -o <your executable binary file>

  4. To run your program just type:

    > ./<your executable binary file>
fig01
Fig 01: Compiling with plain C
  fig02
Fig 02: Compiling with C++

Compiling multi source file C or C++ programs

When compiling programs that span multiple source files (I will use source file and component interchangeable) with gcc you have two options. You can compile all the components and then link them together in one step or you can split the compilation into several steps. The advantages of the first option is that it is simple, easy to understand and use however as your programs grow you will find that sometimes it takes the compiler longer and longer to compile it each time. This is because gcc has to recompile the entire program each time you make a change in one small area. To address this problem you can compile each component separately, then when you change one component just recompile that individual component and link it together with the other previously compiled components to create an executable binary.


Works Cited:

[1] Dennis M. Ritchie. "The Development of the C Language" accessed on 11/20/2003, http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
[2] Bjarne Stroustrup. "Byarne Stroustrup's Homepage" accessed on 11/20/2003, http://www.research.att.com/~bs/homepage.html specifically the FAQ section.


To Top of Page To Top Of Page Printer Friendly Version

©CSG Helpdesk

Valid CSS!