UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

Chapter 14.  The most used options of the gcc compiler

The GNU C compiler gcc is the application that given a set of files with C code, generates an executable program. As with the rest of applications, it can be invoked from the command interpreter. With the option --version the compiler only shows information about its version. Try the following command in the terminal:

$ gcc --version

As all other commands, gcc also has its manual page where its functionality and the options to modify its behavior are explained. To see the complexity of this tool take a look to this page with the command:

$ man gcc

14.1.  From a code file to a program

Create a folder in your user account with name Lab2 and download in it the file main_en.c. Open the file with the editor and check that it contains the main function where execution will start, and an auxiliary function. Next, execute the command to transform this file into a program:

$ gcc main_en.c

Check that in the current directory a file with name a.out has been created. The compiler is designed such that if no name is specified for the resulting program, by default it chooses a.out. This file can be executed as any other command, with the change that the prefix ./ needs to be added because the program is a user folder instead of those where the system searches for programs. Therefore, the command is:

$ ./a.out

The first option we are going to use is precisely that to choose the name of the executable produced by the compiler. This option is -o followed by (separated by a space) the file name that we want to create. For example, if we want to call the program main_en, the command to execute is:

$ gcc -o main_en main_en.c

After executing this second command check that there are now two executable files in the current directory. Delete the one that is no longer needed.