| Main | Linux gnu GDB 2 >| Site Index | Download |


 

 

 

 

 

MODULE 000-1

GCC, G++, GDB AND FRIENDS 1

 

 

 

 

 

 

My Training Period: xx  hours   

 

The skills that should be acquired:

 

 

 

 

 

Note:  The information start with the command and a list of options (including the sub-processes).  Then examples given how to use those commands and some of the options followed by the sample outputs.  Some of the outputs have been trimmed.  It is compiled from the man page.  Run on Linux / Fedora Core 3.

 

The following is a list of file extensions.  Typically, the executable doesn’t have extension.

 

File extension

Description

file_name.c

C source code which must be preprocessed.

file_name.i

C source code which should not be preprocessed.

file_name.ii

C++ source code which should not be preprocessed.

file_name.h

C header file (not to be compiled or linked).

file_name.cc

file_name.cp

file_name.cxx

file_name.cpp

file_name.c++

file_name.C

C++ source code which must be preprocessed.  For file_name.cxx, the xx must both be literally character x and file_name.C, is capital c.

file_name.s

Assembler code.

file_name.S

Assembler code which must be preprocessed.

file_name.o

Object file by default, the object file name for a source file is made by replacing the extension .c, .i, .s etc with .o

The gcc - A GNU Compiler Collection

The following is a list of gcc compiler commands and options.

 

gcc

GNU project C and C++ compiler, a GNU compiler collection.

Usage: gcc [options] file...

Options:

-pass-exit-codes

Exit with highest error code from a phase

--help

Display this information

--target-help

Display target specific command line options

(Use '-v --help' to display command line options of sub-processes)

-dumpspecs

Display all of the built in spec strings

-dumpversion

Display the version of the compiler

-dumpmachine

Display the compiler's target processor

-print-search-dirs

Display the directories in the compiler's search path

-print-libgcc-file-name

Display the name of the compiler's companion library

-print-file-name=<lib>

Display the full path to library <lib>

-print-prog-name=<prog>

Display the full path to compiler component <prog>

-print-multi-directory

Display the root directory for versions of libgcc

-print-multi-lib

Display the mapping between command line options and multiple library search directories

-print-multi-os-directory

Display the relative path to OS libraries

-Wa,<options>

Pass comma-separated <options> on to the assembler

-Wp,<options>

Pass comma-separated <options> on to the preprocessor

-Wl,<options>

Pass comma-separated <options> on to the linker

-Xassembler <arg>

Pass <arg> on to the assembler

-Xpreprocessor <arg>

Pass <arg> on to the preprocessor

-Xlinker <arg>

Pass <arg> on to the linker

-save-temps

Do not delete intermediate files

-pipe

Use pipes rather than intermediate files

-time

Time the execution of each sub process

-specs=<file>

Override built-in specs with the contents of <file>

-std=<standard>

Assume that the input sources are for <standard>

-B <directory>

Add <directory> to the compiler's search paths

-b <machine>

Run gcc for target <machine>, if installed

-V <version>

Run gcc version number <version>, if installed

-v

Display the programs invoked by the compiler

-###

Like -v but options quoted and commands not executed

-E

Preprocess only; do not compile, assemble or link

-S

Compile only; do not assemble or link

-c

Compile and assemble, but do not link

-o <file>

Place the output into <file>

-x <language>

Specify the language of the following input files

Permissible languages include: c c++ assembler none.  'none' means revert to the default behavior of guessing the language based on the file's extension

Options starting with -g, -f, -m, -O, -W, or --param are automatically passed on to the various sub-processes invoked by gcc.  In order to pass other options on to these processes the -W<letter> options must be used.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The following are some of the gcc commands and options examples.

 

[bodo@bakawali testbed5]$ ls -l

total 8

-rw-rw-r--  1 bodo bodo 179 Feb 19 11:35 testbuff.c

 

[bodo@bakawali testbed5]$ gcc -S testbuff.c

[bodo@bakawali testbed5]$ ls -l

total 16

-rw-rw-r--  1 bodo bodo 179 Feb 19 11:35 testbuff.c

-rw-rw-r--  1 bodo bodo 753 Mar 15 14:35 testbuff.s

 

[bodo@bakawali testbed5]$ gcc -c testbuff.c

[bodo@bakawali testbed5]$ ls -l

total 24

-rw-rw-r--  1 bodo bodo  179 Feb 19 11:35 testbuff.c

-rw-rw-r--  1 bodo bodo 1020 Mar 15 14:35 testbuff.o

-rw-rw-r--  1 bodo bodo  753 Mar 15 14:35 testbuff.s

 

[bodo@bakawali testbed5]$ gcc -g -o testbuff testbuff.c

/tmp/cc2wdV8i.o(.text+0x1e): In function `Test':

/home/bodo/testbed5/testbuff.c:8: warning: the `gets' function is dangerous and should not be used.

 

[bodo@bakawali testbed5]$ ls -l

total 36

-rwxrwxr-x  1 bodo bodo 6403 Mar 15 14:38 testbuff

-rw-rw-r--  1 bodo bodo  179 Feb 19 11:35 testbuff.c

-rw-rw-r--  1 bodo bodo 1020 Mar 15 14:35 testbuff.o

-rw-rw-r--  1 bodo bodo  753 Mar 15 14:35 testbuff.s

 

[bodo@bakawali testbed5]$ gcc -g -dumpversion -o testbuff testbuff.c

3.4.2

 

[bodo@bakawali testbed5]$ gcc -g -v -o testbuff testbuff.c

Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.2/specs

Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux

Thread model: posix

gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)

 /usr/libexec/gcc/i386-redhat-linux/3.4.2/cc1 -quiet -v testbuff.c -quiet -dumpbase testbuff.c -auxbase testbuff -g -version -o /tmp/cc4hwP5a.s

ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../i386-redhat-linux/include"

#include "..." search starts here:

#include <...> search starts here:

 /usr/local/include

 /usr/lib/gcc/i386-redhat-linux/3.4.2/include

 /usr/include

End of search list.

GNU C version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3) (i386-redhat-linux)

     compiled by GNU C version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3).

GGC heuristics: --param ggc-min-expand=38 --param ggc-min-heapsize=15786

 as -V -Qy -o /tmp/ccWUyukf.o /tmp/cc4hwP5a.s

GNU assembler version 2.15.92.0.2 (i386-redhat-linux) using BFD version 2.15.92.0.2 20040927

 /usr/libexec/gcc/i386-redhat-linux/3.4.2/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o testbuff /usr/lib/gcc/i386-redhat-linux/3.4.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/3.4.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/3.4.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/3.4.2 -L/usr/lib/gcc/i386-redhat-linux/3.4.2 -L/usr/lib/gcc/i386-redhat-linux/3.4.2/../../.. /tmp/ccWUyukf.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/3.4.2/crtend.o /usr/lib/gcc/i386-redhat-linux/3.4.2/../../../crtn.o

/tmp/ccWUyukf.o(.text+0x1e): In function `Test':

/home/bodo/testbed5/testbuff.c:8: warning: the `gets' function is dangerous and should not be used.

 

 

 

 

 

 

 

 

 

 

 

 

 

If you have more object files to be linked together during the linking process, it is better to create the makefile.  Notice that the assembler options can be passed to gcc such as –g (debug information) etc.  Get some idea at the following links:

  1. Writing makefiles #1

  2. Writing makefiles #2

 

To compile C++ codes you may use the g++g++ accepts mostly the same options as gcc.  The following is a program example.

 

[bodo@bakawali ~]$ cat gccarray.C

 

//**********gccarray.C or gccarray.cpp************

// program to find the total values of an

// array y by passing an array to a function using pointer

#include <iostream>

#define  n  7

using namespace std;

 

// a function prototype

int get_total(int*, int);

 

int main()

{

        int  total,  y[n]={6,9,2,4,5,23,12};

 

        cout<<"\nCalling function get_total(y, n),";

        cout<<"\nBy bringing along the value of y, an array";

        cout<<"\nfirst address and n = 7, an array size.";

        cout<<"\nAn array name, is the pointer to the";

        cout<<"\n1st element of an array\n\n";

 

        // function call, pass along the pointer to the first

        // array element and the array size, and the

        // return result assign to variable total

        total = get_total(y, n);

 

        cout<<"\nSum of the 7 array elements is "<<total<<endl;

        return   0;

}

 

// a function definition

int get_total(int *ptr, int x)

{

        int i, total = 0;

        // do the looping for array elements...

        for(i=0; i<x; i++)

        {

                // displays the array content, pointed by pointer...

                cout<<*(ptr+i)<<" ";

                // do the summing up of the array elements...

                total += *(ptr+i);  //total=total + *(ptr+i);

        }

        // return the result to the calling program...

        return total;

}

 

[bodo@bakawali ~]$ g++ gccarray.C -o gccarray

[bodo@bakawali ~]$ ./gccarray

 

Calling function get_total(y, n),

By bringing along the value of y, an array

first address and n = 7, an array size.

An array name, is the pointer to the

1st element of an array

 

6 9 2 4 5 23 12

Sum of the 7 array elements is 61

[bodo@bakawali ~]$

 

 

 

 

 

 

 

 

 

 

Further related reading:

 

  1. Check the best selling C / C++, Networking, Linux and Open Source books at Amazon.com.

  2. Linux Socket programming tutorial.

  3. C, Buffer overflow and stack frame (construction and destruction).

  4. C, Compiler, Assembler and Linker.

 

 

 

 

 

 

| Main | Linux gnu GDB 2 >| Site Index | Download |


C & Linux GNU Related Utilities:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6