To get started, we're going to get your programming environment set up and make sure that you can compile a simple C program.
One great thing about C and C++ is that there are powerful integrated development environments (or IDEs) to use for every major platform. In the name of simplicity and fundamentals, however, we're going to use the Unix command-line tools in our instructions and in lecture. You are free to use a richly-featured IDE (such as Eclipse) instead of the command-line tools for your own work. Feel free to ask questions on Piazza about these, but the only officially supported development platform will be the Unix command-line tools.
SEAS maintains a Linux cluster, eniac, for all students to use for academic work. Eniac is already equipped with the programs we need to write and compile C/C++ programs, so all we need to do is connect to eniac to get started!
ssh user@eniac.seas.upenn.eduwhere "user" should be replaced with your SEAS username. If you're using one of the programs for Windows, enter eniac.seas.upenn.edu as the server name/address and then connect. Once you are connected to eniac, you will be prompted to enter your username (if you haven't done so already) and your SEAS password. Along the way, you can answer yes to any prompts regarding host identity and saving eniac's host key. If you have difficulties connecting to eniac, see the CETS support entry for connecting to eniac for more information. If that doesn't answer your questions, post on Piazza.
Katie@masks37:~$ ssh gibsonk@eniac.seas.upenn.edu gibsonk@eniac.seas.upenn.edu's password: Last login: Thu Dec 26 18:23:35 2013 from 2607:f470:8:1050:6c0b:e027:6c13:e6e8 gibsonk@plus:~>At this prompt, you can enter commands to navigate your fileshare on eniac or execute programs. Since this is not the Linux skills class, we won't dwell too long on how to be a master command-line user. But here are the basic commands and some example output to get you started:
gibsonk@plus:~> pwd /home1/g/gibsonk gibsonk@plus:~>
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html gibsonk@plus:~> ls -a . .. .bashrc .emacs.dDesktop Documents Download firefox html Mail Public public_html gibsonk@plus:~> ls -l drwx------ 19 gibsonk gibsonk 4096 Jan 26 2011 Desktop drwxr-x--- 3 gibsonk gibsonk 4096 Aug 31 14:00 Documents drwxr-x--- 3 gibsonk gibsonk 4096 Oct 22 2010 Download drwxr-xr-x 11 gibsonk gibsonk 4096 Apr 20 14:58 firefox drwxr-xr-x 9 gibsonk gibsonk 4096 Jan 29 2011 html drwxr-x--- 3 gibsonk gibsonk 4096 Dec 9 2009 Mail drwxr-x--- 2 gibsonk gibsonk 4096 Sep 13 2010 Public lrwxrwxrwx 1 gibsonk gibsonk 4 Sep 10 2008 public_html -> html gibsonk@plus:~>
gibsonk@plus:~> cd Documents gibsonk@plus:~/Documents> pwd /home1/g/gibsonk/Documents gibsonk@plus:~/Documents> cd .. gibsonk@plus:~> pwd /home1/g/gibsonk gibsonk@plus:~>
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html gibsonk@plus:~> touch testfile gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testfile
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testfile gibsonk@plus:~> cp testfile testfile2 gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testfile testfile2
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testfile testfile2 gibsonk@plus:~> mkdir testdir gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir testfile testfile2 gibsonk@plus:~> cd testdir gibsonk@plus:~/testdir> ls gibsonk@plus:~/testdir> cd .. gibsonk@plus:~>
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir testfile testfile2 gibsonk@plus:~> mv testfile2 testfile3 gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir testfile testfile3 gibsonk@plus:~> mv testfile3 testdir gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir testfile gibsonk@plus:~> cd testdir gibsonk@plus:~/testdir> ls testfile3 gibsonk@plus:~/testdir> cd .. gibsonk@plus:~>
gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir testfile gibsonk@plus:~> rm testfile gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir gibsonk@plus:~> rm testdir rm: cannot remove `testdir': Is a directory gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html testdir gibsonk@plus:~> rm -r testdir gibsonk@plus:~> ls Desktop Documents Download firefox html Mail Public public_html gibsonk@plus:~>
gibsonk@plus:~> exit logout Connection to eniac.seas.upenn.edu closed. gibsonk@plus:~$ exit <terminal is now closed>
These commands should be enough to get you through the Unix command-line portion of the class. If you want more information about navigating the command line, you can start with this article from wikibooks. Also, if you're interested in learning more, I recommend taking CIS 191, Unix/Linux Skills, next time it's offered!
In addition to these shell commands, you can also run programs from the command line. One of the programs we will need to run is a text editor to be able to write source code. And the other is a compiler to compile our source code.
If you've set up your computer to allow X-Windows (or you're on a Linux machine already), I recommend using xemacs. You'll need to include the -Y (capital Y) flag when you ssh in to your account. See the end of the Cygwin instructions file for information on how to turn on syntax highlighting (if it's not already on) and fix the indentation style in xemacs. Additionally, all of the commands described for emacs in the "No X-Windows" section below will work the same in xemacs. In particular, "Ctrl-X 1" (Ctrl+X followed by a 1) will change xemacs to fullscreen, instead of the split-screen mode it starts in by default.
There are a plethora of text editors available on eniac. If you are new to the command line, I recommend using emacs. Emacs (rightfully) has a reputation as a complicated beast, but at its core is fairly easy to use, and also comes with syntax highlighting support for C and C++ (and a wide variety of other languages). For example, to run emacs, enter
emacs filenamewhere filename is the name of the file you wish to edit. While emacs offers many commands that can be used to make a programmer's life easier, the basic functionality is fairly standard.
You can navigate a file in emacs with the arrow keys or page up/down. To save the current file, press Ctrl-x Ctrl-s (i.e., first press Ctrl-x then press Ctrl-s). Note that the emacs way of writing this combination of keys is "C-x C-s". To exit emacs, press "C-x C-c". Cut is "C-w", Copy is Esc-w (or Alt-w if you have it and it works in your terminal window), written as "M-w" (M is for meta), and paste is "C-y". "C-s" is search, and "M-%" (Esc-Shift-5) is find and replace (don't ask me why). When you open emacs, it will probably be in split-screen mode. Typing "C-x 1" will switch to fullscreen. ("C-x 2" is split-screen horizontal and "C-x 3" is split-screen vertical.) If you want to learn more about the other features of emacs, you can access a built-in tutorial by typing "C-h t" or search online for one of the many emacs tutorials that are available. Here is a pretty good cheat sheet.
Note that if you are not sshing into eniac (because you are in the Linux lab or on a personal computer), then emacs will appear as a window instead of a command line editor, in which case there are clickable menus with all sort of options.
If you're completely overwhelmed by emacs, you can also try using pico, which can be run just by typing pico at the command line. Pico is simpler than emacs, but not nearly as fun!
Now that you can log in to your eniac account and have access to a text editor, let's write and compile the canonical Hello World program to make sure everything is in order. Create a folder hw1 and navigate into that folder. (At the command line, execute the commands mkdir hw1 and then cd hw1.) Now, make a file hello_world.c that uses printf to print "Hello, world!" to the console.
Though the Hello World program is included in the notes for the first lecture, the code is repeated here for your convenience:
/* File: hello_world.c Author: [your name here] Desc: CIS 190, Homework 1 -- Hello World! in C */ #include <stdio.h> int main() { printf("Hello world!\n"); return 0; }
Compile your hello_world program by invoking gcc as we discussed at the end of the first lecture:
gcc -o hello_world -Wall hello_world.cAnd then run your executable to make sure it does the right thing
gibsonk@plus:~/hw1> gcc -o hello_world -Wall hello_world.c gibsonk@plus:~/hw1> ./hello_world Hello world!Congratulations! You now have a working C development environment and can move onto bigger and better things.
Once you've completed and turned in hello_world.c and answers.txt, take the time to do the following:
Please submit your homework via Canvas. You should submit hello_world.c and answers.txt as a zip or tar file.
If you are unsure how to create a zip or tar of the files that you need to submit, you can follow the instructions below.
delozier@plus:~/homework> ls hw1 delozier@plus:~/homework> ls hw1 answers.txt hello_world.c delozier@plus:~/homework> zip hw1.zip hw1 adding: hw1/ (stored 0%) delozier@plus:~/homework> ls hw1 hw1.zip