Tutorial of gcc and gdb

The graphical user interface (GUI) domainates the current operating environments for personal computing. However, there are still tons of powerful tools, such as gcc and gdb, using the traditional text-based interface. Now, let's turn on the terminal within Linux, FreeBSD, Mac OS X, or any other UNIX-like operating system to discover the power of command-line tools!

*If you don't have any linux machine available, please try to login ieng6.ucsd.edu and ieng9.ucsd.edu with ssh clients. If you have problem login these machines, please contact Hung-Wei.



gcc is the C and C++ compiler developed by GNU project. It is widely adopted as the default compiler of UNIX-like systems. If you are using a Mac, you may also get gcc by installing Xcode (Developer) Tools in the Mac OS X installation Disc #1.

Basic Usage

Assume that we have a C source file "garbage.c" with the content of shown below:

1: #include 
2: #include 
4: int main(int argc, char **argv)
5: {
6:   int a, b, i;
7:   float c;
8:   a = atoi(argv[1]);
9:   b = atoi(argv[2]);
10:  c = a/b;
11:  printf("%d/%d = %f\n",a,b,c);
12:  return 0;
The basic way of compiling garbage.c into an executable file called "garbage" is: If the program is compiled without errors, you can execute the program by typing "./garbage" followed by two numbers as its arguments.

If you are interested about how the assembly code of garbage.c look like, you can also generate the assembly code by replacing the "-o garbage" option with "-S" as:

The gcc will stop compiling the program after the "garbage.s" file is generated. You may use any text editor to browser the content of the assembly code. To figure out what the assembly code does, you may reference the following documents.