Jump to content

CL-09: Compiling C or C++ Programs


Recommended Posts

Browse: [About the FAQ Forum] [Table of Contents] [FAQs] [Contribute] [CL: Command Line Questions]

 

CL-09: Compiling C or C++ Programs

 

It is undeniable that the success of the Free/Libre/Open-Source Software movement has largely been due to GCC, the GNU Compiler Collection. Since Richard Stallman -- the founder of GNU -- released The GNU C Compiler to the world in the late 1980s, it has seen many improvements; the support for many many more languages, and a plethora of architectures.

 

After reading this FAQ, you should be able to:

  • Compile C and C++ programs
  • Get the compiler to warn you about stupid mistakes
  • Link your programs with the required libraries.
  • And finally, optimise your binaries.

This FAQ will not cover the C or C++ languages, they require their own book!

 

The Compilation Process

 

For compiling C and C++ programs, there are two separate compilers to use,

gcc and g++, respectively.

 

Using the famous 'Hello, world!' example (in C) -- hello.c:

 

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   printf("Hello, world!\n");
   return EXIT_SUCCESS;
}

 

We would compile this file into a binary, named 'hello', with the command:

 

 gcc hello.c -o hello

 

This command calls 'gcc,' specifying an input file (you can of course have more than one of these 'translation units'), and an output file -- specified after the -o flag -- called 'hello'.

 

Similarly, in C++ -- hello.cc

 

#include <iostream>

int main(void)
{
   std::cout << "Hello, world!" << std::endl;
   return EXIT_SUCCESS;
}

 

We would compile this file into a binary, named 'hello', with the command:

 

 g++ hello.cc -o hello

 

Very simple, don't you think?

 

Mistakes, and how to catch them

 

We all make mistakes, there's no doubt about that, so having a good way to catch them can make our lives a lot easier. gcc and g++ both provide facilities to do this, the use of a few compiler flags.

 

Both gcc and g++ accept the following flags

  • -W
  • -Wall
  • -ansi
  • -pedantic

-W will enable warnings to help protect you against common mistakes.

 

-Wall will enable extra warnings, if you like.

 

-ansi Will make the compiler reject most features that are not part of the ISO C90 standard (By default GNU extensions are enabled.)

 

-pedantic Will ensure strict conformance to ISO C90, giving no mercy for non-standard features.

 

It is typically a good idea to use all of these flags when compiling your programs, they really do help you discover many possible problems with your code. More information can be found within the gcc man page ('man gcc').

 

 

To link, or not to link, that is the question

 

If you ever want to make any interesting programs, it is likely that you will need to use the linker to link with the libraries of the APIs that you make use of. GCC provides two main facilities to do this.

  • -l
  • -L

Quite intuitive indeed, but what's the difference?

 

-l<libraryname> will search in the default directories for lib<libraryname>.so

 

-L<path> can be used to give an absolute path to the library you want to use, or to add another directory for gcc to search in.

 

A very common usage of -l is to link with the math library in C and C++ programs that use functions from the header <math.h> and <cmath> respectively.

 

To link such a program with the math library, assuming a source file 'program.c', one would issue the command:

 

gcc program.c -o program -lm

 

Easy as that!

 

Optimisation, the double-edged sword?

 

gcc and g++ are optimising compilers, they can be used to increase the speed or size of your compiled binaries. Typically, code that you will be debugging should disable optimisation, but in release-code, you will probably enjoy its benefits.

 

Enabling optimisation is a simple matter, all we need to do is pass an -O (Capital 'Oh'), which has a few main variants.

 

-O0, The default, optimisation-less compile, this is redundant.

-O1, Basic optimisation.

-O2, More optimisations enabled - this is probably the most-used.

-O3, Even more optimisations.

-Os, Size optimisations, this may decrease the size of the output binary.

 

Of course, specific details of these optimisations can be found in the gcc man page.

 

I hope you've enjoyed the FAQ, feel free to ask me any questions if you have trouble.

Edited by Tuxiscool
Link to comment
Share on other sites

 Share

×
×
  • Create New...