Using GCC – the GNU Compiler Collection
GCC has been around since 1987 and was originally the GNU C Compiler. It now compiles several languages and was therefore renamed GNU Compiler Collection. GCC is free and open source, and distributed under the GNU General Public License. Therefore anybody can download, install, use and modify it, and it is the compiler I use for all my C development.
I use Linux on an x86-64 PC, but GCC is available for virtually every architecture you are likely to encounter. It even works on Microsoft Windows but this is frankly a bit of a pain so if you use Windows you might like to consider installing some kind of Linux alongside it as a dual-boot. I use Linux Mint but there are plenty to choose from, all free. If you use Linux you might have gcc already; to find out type the following in the terminal:
If you have gcc installed you’ll see something like
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If not you will get
The program ‘gcc’ is currently not installed. You can install it by typing:
sudo apt-get install gcc
in which case do just that! Open the terminal and enter
sudo apt-get install gcc
For all my posts on this site I will give the necessary command to compile the code but a brief overview of the options is worthwhile.
The most basic usage of GCC is as follows
Assuming there are no errors in the code this will produce a file called a.out, which you can run using
You’ll probably want a less generic name, so use the -o option to specify the output name
gcc xyz.c -o xyz
If you have more than one source code file just list them after gcc
gcc xyz.c abc.c -o xyz
Finally, there have been a number of C standards over the years and if you use features of later versions you’ll need to specify that to gcc. The latest version is C11, so to inform the compiler you are using some of its features use -std=c11.
gcc xyz.c abc.c -std=c11 -o xyz
That’s a very brief introduction to GCC, enough to understand the commands I have used so far. I will update this page when necessary so it always explains any commands I have listed in any of my posts.
Very quick update – if you use
you must use -lm when compiling, eg
gcc xyz.c abc.c -lm -o xyz
I will explore this in more detail at a later date.