Compiling is the process of transforming a high level language into a low level langauge. A high level langauge is closer to English. A low level language is closer to what the computer understands.

Compiling a C Program

  1. Compiling is the transformation from Source Code (human readable) into machine code (computer executable). A compiler is a program. A compiler takes the recipe (code) for a new program (written in a high level language) and transforms this Code into a new language (Machine Language) that can be understood by the computer itself. This "machine language" is difficult to impossible for humans to read and understand (much less debug and maintain), thus the need for "high level languages" such as C.

  2. The compiler also ensures that your program is TYPE correct. For example, you are not allowed to assign a string to an integer variable!

  3. The compiler also ensures that your program is syntactically correct. For example, "x * y" is valid, but "x @ y" is not.

  4. The compiler does not ensure that your program is logically correct.

  5. The compiler we use is the GNU (Gnu is not Unix) Open Source compiler.

    G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.).

    To compile a program, you use the following command:

        % g++ -g -pedantic -Wall -o executable_file_name source_file_name.C

    This command can be written at the Linux command window, or can be typed in using emac's compile command

Parts of the Compile Command Syntax

Compilers provide many options and settings that you can use depending on what properties you want the compiled program to have (e.g., faster vs. easier to debug).

Again, remember we use the following command to compile a program:

    % g++ -g -pedantic -Wall -o executable_file_name source_file_name.C

The options we will use for g++ are:

  1. g++ : (the name of compiler)
  2. -g : (allow debugging)
  3. -pedantic : (only allow real C)
  4. -Wall : (provide all warnings of possible mistakes)
  5. -o "X" : name the exectuable X
  6. file.C : the source code
  7. there are many (MANY) more options, but few that we will use in CS1000. These have to do with optimizations, specific computer architectures, etc...

A Note on the G++ (GNU) Compiler

There are many compilers for C, but we will focus on a free open source version called the Gnu C compiler. (Actually we will use the Gnu C++ compiler, but all C programs compile using this compiler).

The g++ compiler is open source, meaning you can use it for free on any project you want, including "for profit" projects. Further, if you so desire, you could extend the compiler to work better, fix bugs in the compiler, port the compiler to another operating system/computer architecture, etc.

G++ will compile not only C++ programs, but C programs as well!

You can download G++ free of charge for your home machine. It will run under Linux or Windows. The most recent version of the compiler can be found here: Gnu Web Page

Additional documentation on the compiler is available at this location as well.

Back to Topics List