GNU Compiler Collection

gcc and g++ are both part of the GNU Compiler Collection. g++ is a version of GCC with pre-defined options for C++ code.

You install them, and everything necessary for devs, using:

$ sudo apt-get install build-essential # gcc, cmake...
$ sudo apt-get install manpages-dev    # man pages for devs

➑️ We usually use CMake and Makefiles to automate the compilation.


GCC/G++ Usage

Basic usage

Compile and generate an executable a.out.

$ gcc file.c
$ g++ file.cpp

Common usage

Partially compile files that depend on each other (#include), then assemble them in an executable a.out.

$ gcc -c file1.c       # generate "file1.o" object file
$ gcc -c file2.c       # generate "file2.o" object file
$ gcc file1.o file2.o  # generate "a.out"

➑️ For g++: replace gcc with g++ and .c with .cpp.

πŸš€ One-line command: gcc -c *.c && gcc *.o && rm *.o

Other options

Commonly used options:

  • -o: set the executable name (default=a.out)
  • -std=value: specify a standard such as c99 or c++11
  • -I/path/to/xxx: include a folder with header files
  • -g: add information for debuggers (ex: GDB)

Common compliance/warnings options:

  • -Wall: enable compiler warnings
  • -Wextra: enable additional compiler warnings
  • -Werror: mark compiler warnings as compilation errors
  • -Wpedantic: enable strict ISO conformance
  • -ansi: C89 compliance (rarely used)

Less commonly used options:

  • -E: generate intermediate files
  • -S: generate assembly code

πŸ‘» To-do πŸ‘»

Stuff that I found, but never read/used yet.

  • -llib
  • -L/path/to/lib
  • -Z ...
  • -MM (deps)