gcc

Getting started with gcc

Remarks#

GCC (upper case) refers to the GNU Compiler Collection. This is an open source compiler suite which include compilers for C, C++, Objective C, Fortran, Ada, Go and Java. gcc (lower case) is the C compiler in the GNU Compiler Collection. Historically GCC and gcc have been used interchangeably, but efforts are being made to separate the two terms as GCC contains tools to compile more than C.

Documentation in this section will refer to gcc, the GNU C compiler. The intent is to provide a quick lookup of common actions and options. The GCC project has detailed documentation at https://gcc.gnu.org which document installation, general usage, and every command line option. Please refer to the official GCC documentation on any question not answered here. If a certain topic is unclear in the GCC documentation, please request specific examples.

Versions#

VersionRelease Date
7.12017-05-02
6.32016-12-21
6.22016-08-22
5.42016-06-03
6.12016-04-27
5.32015-12-04
5.22015-07-16
5.12015-04-22
4.92014-04-22
4.82013-03-22
4.72012-03-22
4.62011-03-25
4.52010-04-14
4.42009-04-21
4.32008-03-05
4.22007-05-13
4.12006-02-28
4.02005-04-20
3.42004-04-18
3.32003-05-13
3.22002-08-14
3.12002-05-15
3.02001-06-18
2.951999-07-31
2.81998-01-07
2.71995-06-16
2.61994-07-14
2.51993-10-22
2.41993-05-17
2.31992-10-31
2.21992-06-08
2.11992-03-24
2.01992-02-22
1.421992-09-20
1.411992-07-13
1.401991-06-01
1.391991-01-16
1.381990-12-21
1.371990-02-11
1.361989-09-24
1.351989-04-26
1.341989-02-23
1.331989-02-01
1.321988-12-21
1.311988-11-19
1.301988-10-13
1.291988-10-06
1.281988-09-14
1.271988-09-05
1.261988-08-18
1.251988-08-03
1.241988-07-02
1.231988-06-26
1.221988-05-22
1.211988-05-01
1.201988-04-19
1.191988-03-29
1.181988-02-04
1.171988-01-09
1.161987-12-19
1.151987-11-28
1.141987-11-06
1.131987-10-12
1.121987-10-03
1.111987-09-05
1.101987-08-22
1.91987-08-18
1.81987-08-10
1.71987-07-21
1.61987-07-02
1.51987-06-18
1.41987-06-13
1.31987-06-10
1.21987-06-01
1.11987-05-24
1.01987-05-23
0.91987-03-22

“Hello world!” with common command line options

For programs with a single source file, using gcc is simple.

/* File name is hello_world.c */
#include <stdio.h>

int main(void)
{
    int i;
    printf("Hello world!\n");
}

To compile the file hello_world.c from the command line:

gcc hello_world.c

gcc will then compile program and output the executable to the file a.out. If you want to name the executable, use the -o option.

gcc hello_world.c -o hello_world

The executable will then be named hello_world instead of a.out. By default, there are not that many warnings that are emitted by gcc. gcc has many warning options and it is a good idea to look through the gcc documentation to learn what is available. Using ‘-Wall’ is a good starting point and covers many common problems.

gcc -Wall hello_world.c -o hello_world

Output:

hello_world.c: In function ‘main’:
hello_world.c:6:9: warning: unused variable ‘i’ [-Wunused-variable]
     int i;
         ^

Here we see we now get a warning that the variable ‘i’ was declared but not used at all in the function.

If you plan to use a debugger for testing your program, you’ll need to tell gcc to include debugging information. Use the ‘-g’ option for debugging support.

gcc -Wall -g hello_world.c -o hello_world

hello_world now has debugging information present supported by GDB. If you use a different debugger, you may need to use different debugging options so the output is formatted correctly. See the official gcc documentation for more debugging options.

By default gcc compiles code so that it is easy to debug. gcc can optimize the output so that the final executable produces the same result but has faster performance and may result in a smaller sized executable. The ‘-O’ option enables optimization. There are several recognized qualifiers to add after the O to specify the level of optimization. Each optimization level adds or removes a set list of command line options. ‘-O2’, ‘-Os’, ‘-O0’ and ‘-Og’ are the most common optimization levels.

gcc -Wall -O2 hello_world.c -o hello_world

‘-O2’ is the most common optimization level for production-ready code. It provides an excellent balance between performance increase and final executable size.

gcc -Wall -Os hello_world.c -o hello_world

‘-Os’ is similar to ‘-O2’, except certain optimizations that may increase execution speed by increasing the executable size are disabled. If the final executable size matters to you, try ‘-Os’ and see if there is a noticeable size difference in the final executable.

gcc -Wall -g -Og hello_world.c -o -hello_world

Note that in the above examples with ‘-Os’ and ‘-O2’, the ‘-g’ option was removed. That is because when when you start telling the compiler to optimize the code, certain lines of code may in essence no longer exist in the final executable making debugging difficult. However, there are also cases where certain errors occur only when optimizations are on. If you want to debug your application and have the compiler optimize the code, try the ‘-Og’ option. This tells gcc to perform all optimizations that should not hamper the debugging experience.

gcc -Wall -g -O0 hello_world.c -o hello_world

‘-O0’ performs even less optimizations than ‘-Og’. This is the optimization level gcc uses by default. Use this option if you want to make sure that optimizations are disabled.

Determine gcc version

When referring to gcc’s documentation, you should know which version of gcc you are running. The GCC project has a manual for each version of gcc which includes features that are implemented in that version. Use the ‘-v’ option to determine the version of gcc you are running.

gcc -v

Example Output:

Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC)

In this example we see that we are running gcc version 5.3.1. You would then know to refer to the GCC 5.3 manual. It is also helpful to include your gcc version when asking questions in case you have a version specific problem.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow