4 Compiling a program

Let us use a sample program, as follows:

#include <stdio.h>
int main(void)
{
  int i;

  for (i = 10; i > 0; --i)
  {
    printf("countdown %d\n",i);
  }
  printf("Hello world!\n");
  return 0;
}

This program is simple, but it suffices as an example.

Once you have this program typed up in an editor, be sure to save the file as filename.c, the .c extension is important. For now, we'll assume the file is aaved as test.c. Note that the content of test.c cannot be recognized by a computer natively. The program needs to be translated to the equivalent object code.

Issue the following command in a Linux CLI:

gcc -g -c test.c

This command invokes the compiler gcc. The -c option specifies the output should be an object file, and it should be called test.o.

You can verify the existence of test.o using the ls command.



Copyright © 2006-08-28 by Tak Auyeung