CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
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.
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.
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.
Create another simple C program that prints out some other message. Compile it and run it.
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.
Try cutting out different parts of the program and see what happens when you try to compile it. For example,
#include line.
f from printf.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
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