Beginner's GCC Tutorial

INTRODUCTION TO GCC

The GNU Compiler Collection (GCC) is a collection of compilers for the programming languages C, C++, Objective-C, Fortran, Ada, Java and Google Go. GCC generally comes pre-installed on most of the Linux distributions. The pre-installed GCC normally supports the C programming language only. The user can install compiler frontends for other languages like C++, Fortran etc using any standard package manager. 

In this tutorial we will concentrate specifically on the compilation process of C programs. The compilation processes for the other languages are more or less the same. 

CONTENTS

  1. BASICS OF GCC
  2. -o OPTION : NAMING THE EXECUTABLE FILE
  3. -c FLAG : COMPILE TO OBJECT CODE ONLY
  4. MULTIFILE COMPILATION
  5. LINKING LIBRARIES
  6. MAKING SHARED LIBRARIES : -shared FLAG
  7. OPTIMIZATION : -O FLAGS
  8. USE DIFFERENT STANDARDIZATION : -std OPTION
  9. THE WARNING FLAGS
  10. ENABLE DEBUG : -g FLAG  

BASICS OF GCC

On any regular Linux environment you can always check manual pages for any tool using the man command or the info command. GCC is no different. You can type the following on the terminal command prompt and check the manual page for GCC.


$ man gcc

alternatively you can use info too

$ info gcc

In both cases you get something like the following image. The man pages are scrollable, you can scroll down the page and keep reading. The man pages are always highly informative from the developer point of view.





For checking options available with GCC, you can use the --help option with GCC on the terminal command line. 



Now let's explore some of the basic command line options for GCC. To start with, apart from the --help option, let's consider the --version option. This option outputs the version of the GCC installed on your machine. In my case it's GCC 4.6.4 installed on my Ubuntu12.04LTS based GNEOS operating system (GNEOS is a heavily customized and personalized make of ElementaryOS Luna, which is in turn based on Ubuntu12.04LTS. I'll cover how to customize, optimize, patch and build your own Linux Kernel from source and how to tweak with almost everything in a Linux System to make your own OS in another set of tutorials.) 



-o OPTION : NAMING THE EXECUTABLE FILE

Another useful and basic option is the -o flag. When you compile a source program with GCC the default name for the executable is a.out . But the -o flag lets you create your executable with a name decided by you. The syntax is 

$ gcc -o <Name of Executable>  <Name of Source File> 

Remember that in Linux file extensions mean practically nothing. You can make executables with no extensions at all. You can even name your executable as someExecutable.mp3 and it'll still work. 

Let's do an example. Here is a C program that takes two integers as input from the user and prints their sum. We name the source file as addTwoInts.c .



Now we compile it with no options.

$ gcc addTwoInts.c

This will produce an executable named a.out which will read two integers and print their sum. The executable can be run by entering ./a.out on the command line. (Try it yourself)

Now if we want to name the executable something else, let's say 'addTwoInts.executable' , we would have to use the -o flag as the following.

$ gcc -o addTwoInts.executable addTwoInts.c

Now you can run your executable by entering the following in the command line


$ ./addTwoInts.executable


-c FLAG : COMPILE TO OBJECT CODE ONLY

Another useful option is the -c flag. It compiles the file to produce only the object code, not the executable. Then the object files can be linked together manually to create the executable. This approach becomes highly helpful while dealing with large projects with multiple source files. The default file name for the object file is same as that of the source file, only the .c extension is replaced by an .o extension. You can of course name it something else by using the -o flag as described above.

$ gcc -c addTwoInts.c        # this will produce addTwoInts.o object file 

If I want to compile my code to an object file and name it addTwoInts.obj I would write


$ gcc -c addTwoInts.c -o addTwoInts.obj 


MULTIFILE COMPILATION

You can also compile multiple files together with GCC, this is as simple as compiling a single file. You simply write the names of the source files separated by space. For example

$ gcc -o someExecutable sourceCodeOne.c sourceCodeTwo.c sourceCodeN.c

This will compile all the source files given by you, link them and give you the executable. We shall discus more on multifile compilation and build strategies for large projects in tutorials for Makefile.


LINKING LIBRARIES

In some cases, we have useful functions in libraries and to use those functions we need to link those libraries to our program. For example, the following program needs the math library to be linked.


The flag to link the math library is -lm . While linking libraries generally the flags are given at the end of the command. Here in this case, the command would be

$ gcc math_example.c -lm

If we do not use the -lm flag to link the math library, then we'll get an error about the sine function not being defined. The library flags always start with 'l' and have the name of the library in it. For example, the flag for linking pthread library is -lpthread, flag for linking sdl is -lsdl. You can even create your own libraries and use -l<your lib name> to make gcc link it to your code. The process of creating libraries will be discussed in the advanced tutorial.

0 Read More »

Total Pageviews