CSC161 2011S Imperative Problem Solving

Laboratory: Compiling C

Summary: We cover the basics of compiling C programs in a Linux environment.

Prerequisites: Familiarity with the MathLAN network. Familiarity with basic Linux commands. Ability to use an editor.

Preparation

a. Log in to your MathLAN workstation. (Of course, you've probably already done that if you're reading this laboratory.)

b. Open a terminal window into which you can type commands.

c. Create a directory for this lab. I would recommend csc161/labs/compiling-c.

Exercises

Exercise 1: Hello World

The first program that we often write in a new programming language is Hello World, which simply prints that string. Here is that program in C.

#include <stdio.h>

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

a. Put that code into the file hello.c. You can use gedit to create the file. You can also use cat.

b. We compile files with the cc command. Type

cc hello.c

If all goes well, you should see no comments at all from the system.

However, you should find that the compilation created a file. Determine the name of that file.

c. As you may have just discovered, by default, the C compiler creates an executable file named a.out. You can run that file by typing

./a.out

Try doing so now.

Exercise 2: A Second Program

Create another simple C program that prints out some other message. Compile it and run it.

Exercise 3: Choosing the Executable Name

As you may have discovered, one disadvantage of the way C compiles files is that all executables end up with the same name, a.out. Fortunately, there is a flag you can give to the C compiler that tells it what output file name to use. In particular, you can write -o filename to choose the output file.

a. Type

cc -o hello hello.c

b. Verify that this created the file hello.

c. Run it with ./hello.

d. Do the same with the program you created in exercise 2.

Exercise 4: Errors

Try cutting out different parts of the program and see what happens when you try to compile it. For example,

 

History

Monday, 31 January 2011 [Samuel A. Rebelsky]

  • Created.

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Jan 31 10:49:50 2011.
The source to the document was last modified on Mon Jan 31 10:49:49 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/compiling-c-lab.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/compiling-c-lab.pdf

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.