On Unix-like operating systems, the cc command runs the system’s C compiler.

This page covers the GNU/Linux version of cc, a symbolic link to gcc, the GNU compiler collection.

Description

The compiler’s essential duty is to translate a computer program from one language to another, and it usually does this more than once in a single compilation.

  • Description
  • Syntax
  • Options
  • Environment variables
  • Examples
  • Related commands
  • Linux commands help

When compiling a program written in C, the compiler goes through several steps to translate it into machine code that can be executed on your target architecture or platform.

In general, the compiler performs the following functions on your code:

  • Pre-processing. The C preprocessor (“cpp”) prepares your code to be sent to the actual compiler by removing comments, expanding macros, and performing a few other important steps.
  • Syntax checking. Does your program contain any syntactical errors? If so, the compiler lets you know, and you need to fix them before you proceed. If it suspects some parts of your program are not correct, but it’s not sure that they are actually errors, it may issue warnings, but still compiles the program.
  • Conversion to assembly language. This is an intermediary step where your code is translated into the language of your target system’s assembly language. This is pretty much the last step where your code is still readable by a human.
  • Assembly. Your program is assembled into the bits and bytes of object code suitable for your target platform.
  • Linking. The object code is linked together with the object code of any library functions specified by your program and command line options, and an executable file is created.

Syntax

cc [ options ]

Options

There are literally hundreds of options you can pass to the compiler to fine-tune how your program executes. Here we have selected some useful and commonly-used options which you may need to create an executable file.

Environment variables

The following environment variables affect the compilation process:

Examples

cc myfile.c

Compile the file myfile.c. Output will be written to the executable file a.out.

cc myfile.c -o myexe

Compile the file myfile.c and name the compiled executable output file myexe.

cc myfile.c -Wall -o myexe

Compile the file myfile.c and output the compiled executable as myexe, displaying warnings during compilation if they occur.

cc myfile.c -Wall -lX11 -o myexe

Compile myfile.c to the executable myexe, linking the libX11 library and issuing any applicable warnings during compilation.

cc myfile.c -Wall -ansi -lX11 -o myexe

Compile myfile.c to the executable myexe, linking the libX11 library, adhering strictly to ANSI C standards, and issuing warnings if applicable.

ld — Link editor for object files.ctags — Create tag files for source code.