CP264 : Notes - C Compilation
- C is a general-purpose programming language initially developed by Dennis Ritchie between 1969-1973 at AT&T Bell Labs
- American National Standards Institute (ANSI) approved the first C standard (known as ANSI C or C89) in 1989
- International Organization for Standardization (ISO) proved a revised standard (known as C99) in 1999
- The current version of the standard (known as C11) was approved in 2011
- C is one of the most widely used programming languages. C compilers are available for majority of computer architectures and OSs
- Some other commonly used programming languages are based on C:
- C++ (Bjarne Stroustrup 1979)
- Java (James Gosling 1984)
- Objective C ( Brad Cox and Tom Love after 1981)
Characteristics of C
- A high level compiled programming language
- Small size: C has only 32 keywords
- Makes extensive use of function calls, well suited for structured programming (Not for objected-oriented programming)
- Supports low level (bitwise) programming
- Supports pointers to refer computer memory, array, structures and functions
- Stable, portable and extensible, but not as easily portable as interpreted languages such as Java and Python
Uses of C
- Primarily used for system programming
- The portability, efficiency, and the ability to access specific hardware addresses and low runtime demand on system
- Has been so widely accepted by professionals that compilers, libraries, and interpreters of other programming languages are often implemented in C
- For portability and convenience reasons, C is sometimes used as an intermediate language by implementations of other languages
- Widely used to implement end-user applications
C program structure
C source code [pre-processor statements] [declare global variables and constants] [declare function prototypes] int main(argument(s)) { [program statements] return 0; } [implementation of functions]
Generally, header information (pre-processor statements, global variables,
constants, and function prototypes) are put into separate header (.h
)
files, while the function implementations are placed in one or more source
code (.c
) files. If source code has been compiled into a
library, the associated header files describe access to the library
functions.
First C Program
// This is my first program in C #include <stdio.h> int main(void) { printf("Hello world\n"); return 0; }
File Name Extension Convention
- .c
- C language source file
- .h
- C language header file
- .o
- object file
- .s
- assembly language source file
- .exe
- executable (for Windows)
- .out
- executable (for Unix/Linux)
- .lib
- static library (for Windows)
- .a
- static library (for Unix/Linux)
- .dll
- dynamic library (for Windows)
- .so
- dynamic library (for Unix/Linux)
Compilation, Linking, and Execution
The Steps
- pre-processing
- compilation
- assembly
- linking
- generation of an executable program file (a program in machine code)
Compiling
We use GNU (GNU's Not Unix
) C compiler
gcc
is GNU C compilerg++
is GNU C++ compiler
Simple Command Line Example:
gcc helloworld.c
-
Pre-Processing
Resolve lines Pre-processor directives (lines that start with
#
), are resolved. Examples:#include <stdio.h> #define STACK_INIT 8
-
Compilation
Convert the C program into an assembly program. (This generally does not create a file.)
-
Assembly
Convert the assembly code to binary format, called an object file. (This creates a
.o
file.) -
Linking
Link the object file with other library files
-
Executable Generation
From the linked code, create an executable program file. (
.exe
or.out
files.)
The resulting executable program can then be loaded into memory and executed.
Compiler and Linker Options
There are a large number of compiler and linker options available. These options give the compiler / linker instructions on how to compile and link the source code. They can be set on the command line, in make files (files that you can use to set instructions for the compiler / linker), or in IDE (i.e. Eclipse) settings. The following are a few examples:
-
-c
-
(compile) suppresses linking step of compilation, but generates an object file
gcc -c helloworld.c
generates
helloworld.o
-
-g
-
(gdb) embeds diagnostic (debugging) info in the object file. Still creates an executable, but allows you to walk through the executable with a debugger program.
gcc -g helloworld.c
generates
a.exe
-
-o object
-
(output) names the executable program from linking instead of the default name
gcc -o hello helloworld.c
generates an executable file named
hello.exe
-
-Wlevel
-
(warnings level) give all compiling warnings
gcc -Wall helloworld.c
Show all warnings.
-
-Idirectory
-
(include file path) give the list of directories for directive include files, if these files are not in the current directory
-
-larg
-
(libraries) Link with the library file named
libarg.lib
. By default search/lib
and/usr/lib
(or other default library directories) for this library -
-Ldirectory
-
(Library search path) Search for the library
libarg.lib
from the above option in the directory directory. -
-std=standard
-
(standard) Set the language standard for compilation.
gcc -std=c99 helloworld.c
Compile to the C99 standard