5 UNIX / Linux cc Command Examples

What is cc?
5 cc examples
Syntax and Options
Related Commands

What is cc?

The cc compiler is the default compiler for Unix systems like Sun solaris. This is a powerful compiler used for compilation of C code.

5 cc Examples

1. A basic example

Compiling a C code with cc compiler is very easy. For a very basic case, just pass the input C file as an argument to cc compiler.

$ cc helloworld.c

The above command would produce a file with name a.out

2. Show each component as invoked using -###

This option makes the cc compiler to invoke all the compilation stages but doesn’t actually compile the code. Its more like a test simulation.

cc -### helloworld.c

After executing the above command, if the ls command is run:

$ ls a.out
ls: cannot access a.out: No such file or directory

So we see that actual compilation did not take place.

3.Prevent removal of comments using -C

Suppose your code is like :

#include<stdio.h>

int main(void)
{
    printf("\n Hello World \n");
    return 0; //return from main
}

In a normal compilation, the comment above would be removed. But if -C option is used with cc compiler then the comment would get preserved.

4. Create an object file using -c

An object file (that can be linked with other object files) can be produced using the -c flag.

cc -c helloworld.c

The above command would produce an object file named helloworld.o

5. Define a command line macro using -D

Sometimes it is desired to control compilation of some pieces of code from command line. Well, this can be done using -D option along with macro name. This defines the macro for code.

#include<stdio.h>

int main(void)
{
#ifdef L101
    printf("\n Hello World \n");
#endif
    return 0;
}

So, to define a macro from command line :

cc -DL101 helloworld.c

The command above will define the macro L101.

Syntax and Options

cc  [  -#  ]  [  -###  ]  [  -Aname  [(tokens)  ]  ]  [   -B
     [static|dynamic] ] [ -C ] [ -c ] [ -Dname [=tokens ] ]
     [ -d [y|n] ] [ -dalign ] [ -E ] [ -erroff=t ] [ -errtags=a ]
     [ -errwarn=t ] [ -fast ] [ -fd ] [ -flags ]
     [ -fnonstd ] [ -fns=yes|no ] [ -fprecision=p ] [ -fround=r ]
     [ -fsimple[=n] ] [ -fsingle ] [ -fstore ]
     [ -ftrap=t ] [ -G ] [ -g ] [ -H ] [ -h name ] [ -Idir ] [ -i
     ] [ -keeptmp ] [ -KPIC ] [ -Kpic ] [ -Ldir ]
     [ -lname ] [ -mc ] [ -misalign ] [ -misalign2 ] [  -mr  ]  [
     -mr,string ] [ -mt ] [ -native ] [ -nofstore ]
     [ -noqueue ] [ -O ] [ -o filename ] [ -P ] [ -p ] [ -Q [y|n]
     ] [ -qp ] [ -Rdir[:dir] ] [ -S ] [ -s ]
     [ -Uname ] [ -V ] [ -v ] [ -Wc,arg ] [ -w ] [ -X [a |c |s |t
     ]] [ -x386 ] [ -x486 ] [ -xa ] [ -xarch=a ]
     [ -xautopar ] [ -xcache=c ] [ -xCC ] [ -xcg89 ] [ -xcg92 ] [
     -xchar_byte_order=o ] [ -xchip=c ]
     [ -xcode=v ] [ -xcrossfile[=n] ] [ -xdepend  ]  [  -xe  ]  [
     -xexplicitpar ] [ -xF ] [ -xhelp=f ] [ -xildoff ]
     [ -xildon ] [ -xinline=v ] [ -xlibmieee ]  [  -xlibmil  ]  [
     -xlic_lib_=sunperf ] [ -xlicinfo ]
     [ -xloopinfo ]
     [ -xM ] [ -xM1 ] [ -xMerge ] [ -xmaxopt=[off,1,2,3,4,5] ]  [
     -xmemalign=ab ] [ -xnolib ]
     [ -xnolibmil ] [ -xO[1|2|3|4|5] ] [ -xP ] [ -xparallel  ]  [
     -xpentium ] [ -xpg ] [ -xprefetch=[val],val ]
     [ -xprofile=p ] [ -xreduction ] [ -xregs=r ] [  -xrestrict=f
     ] [ -xs ] [ -xsafe=mem ] [ -xsb ] [ -xsbfast ]
     [ -xsfpconst ] [ -xspace ] [ -xstrconst ] [ -xtarget=t  ]  [
     -xtemp=dir ] [ -xtime ] [ -xtransition ]
     [ -xunroll=n ] [ -xvector ]  [  -xvpara  ]  [  -Yc,dir  ]  [
     -YA,dir ] [ -YI,dir ] [ -YP,dir ] [ -YS,dir ]
     [ -Zll ] [ -Zlp ]
Short Option Long Option Option Description
-# Show each component as it is invoked (verbose mode).
-Aname[(tokens)] Associate name as a predicate with the specified tokens as if by a #assert preprocessing directive.
-B [static|dynamic] Specifies whether bindings of libraries for linking are static or dynamic, indicating whether libraries are non-shared or shared, respectively. -B dynamic causes the link editor to look for files named libx.so and then for files named libx.a when given the -lx option. -B static causes the link editor to look only for files named libx.a. This option may be specified multiple times on the command line as a toggle. This option and its argument are passed to ld.
-C Prevents the C preprocessor from removing comments, other than those on preprocessing directive lines
-c Directs the C compiler to suppress linking with ld and to produce a .o file for each source file. You can explicitly name a single object file by using the -o
option. When the compiler produces object code for each or input file, it always creates an object file in the current working directory. If you suppress the linking
step, you also suppress the removal of the object files.
-d [y|n] -dy specifies dynamic linking, which is the default, in the link editor. -dn specifies static linking in the link editor.
-dalign (SPARC) -dalign is equivalent to -xmemalign=8s. For more information, see -xmemalign.
-E Preprocesses only the named C files and sends the result to the standard output. The output contains preprocessing directives for use by the next pass of the compilation system. See also -P option.

Related Commands

as
dbx
ild
ld
lint
prof
tmpnam