Click for Table of Contents
The C Primer
Book and Reference Collection
Copyright © 2024 by Rance D. Necaise
Table Of Contents
The C Primer
Copyright © 2024
Rance D. Necaise

1.3 Program Compilation

As indicated earlier, C is a compiled language. It must be converted or translated from the high-level syntax into executable code for a specific processor. The result is known as an executable program. This translation, known as compiling, is performed using a compiler.

Compiling and Linking

The GNU Compiler Collection[1] is typically used on Linux computer systems to create executable programs. This software collection provides command line applications for creating executable programs from a variety of programming languages. In this text, we will use the g++ GNU compiler. This application can be used to compile both C and C++ programs. Although, this text is focused on the C programming language, as indicated earlier there may be times when you will need to work with the C++ language.

Creating an executable C program involves a two step process, compilation and linking. For simple programs consisting of a single source file, the two steps can be combined using a single command:

g++ -o hello -Wall hello.cc

The -o option specifies the name of the executable program created from compiling and linking the source file hello.cc. The executable file is created within the current directory.

WARNING
WARNING

Be very careful using this command. It is not unusual to accidentally enter something similar to

    g++ -o mypgm.cc -Wall mypgm.cc

instead of that specified above. If you make this mistake, there will be no warning and the compiler will create the executable named mypgm.cc which overwrites your sourcefile.

Execution

The compilation command above, simple creates the executable program. To execute the program, you must enter the name of the executable program at the command-line the same as you would with a Linux system command:

./hello

Remember, executable files in Linux do not have a special extension as they do in Windows. Thus, you only provide the name of the executable in order to execute the program.

NOTE
NOTE

For security reasons, the Linux shell only looks in specific system directories for executables. To execute a program located outside of the system directories, you must provide an absolute or relative path to the file. Here, the ./ is used to indicate that the executable file is in the current directory.

Syntax Errors

Page last modified on February 10, 2024, at 03:03 PM