Title: CS 325
1CS 325
- Unix Programming Environment
- and
- Windows Programming
- with Microsoft Foundation Classes (MFC)
2Announcements
- Any student wishing to go through interviews for
internships or full time positions who has not
already registered with the Engineering Career
Services office, should do so by January 20.
Students must go through a one hour orientation
program in order to be fully registered with
ECS. Students can go by 130 MIB to sign up for a
time, or call the office at 348-6382.
3CS Supplemental Instruction
Association for Computing Machinery
- Start the semester off right!
- Dont get behind!
- Ask questions early!
Where AIME Building Behind Ferg. Parking Lot
Across from Paty Hall Room 111/112 When CS
325 Wednesday Night 6-7pm Thursday 630-730pm
NEW TIMES! NO CHARGE! CS325 Leader Hayden Hayes
4Lecture 5
- Today
- Unix programming environment
- vi
- make
- ftp
- Homework
- Reading Assignments
- Unix
- vi, emacs, make, ftp
- http//bama.ua.edu/cgi-bin/cgiwrap/unixinfo/chsh.
pl - Make sure you have an normal, interactive shell
(ksh/bash) - Sign up for an account on unix.eng.ua.edu
(required) - For Fun http//www.catb.org/esr/writings/taoup/h
tml/
5Unix
6Accessing Bama
- Two general techniques exist
- If at a console, X-windows is preferred
- Windowing system similar to Mac or Windows
- variants of windowing system developed at Xerox
PARC - Can log into OX (unix.eng) from Houser (back
wall) - If on another system, telnet to log on
- Remote connection
- Graphical remote connections exist (eg VNC)
- Not efficient in general
- Start/Run/telnet bama.ua.edu
- telnet is not secure if security is an issue
use putty - LOG ONTO BAMA NOW(telnet bama.ua.edu)
7Learning about Unix
- Basic commands
- File system structure
- Networking Mail
- Editors
- Compilers
- Unix is case-sensitive !
8Basic Commands
- ls lists files in your current directory
- -C output is in columns-F displays info on the
type of file (/ _at_ )-a displays system (hidden)
files-R recursive, lists from this directory on
down - cp copy, cp sourcefile destinationfile
- mv move, mv sourcefile destinationfile
- rm delete, rm filename
- mkdir create directory, mkdir CS325
- rmdir remove directory, rmdir Project1
9Basic Commands (continued)
- cat displays a file, cat filename
- more displays file one screen at a time
- grep finds string in file, grep string file(s)
- pipe, output from command sent to another
- ls wc
- wildcard, matches zero or more characters
- rm
- ls a b c
- ? wildcard, matches single character
10File System Structure
- Tree-based directory structure
- / is the root of the tree (forward-slash)
- represents your home directory
- . Represents your current working directory
- .. Represents the parent directory of .
- pwd shows the directory you are currently in
- cd change directory (.. is parent directory)
- Can give either an absolute or relative directory
- Absolute, cd /fs/truitt (full path from root)
- Relative, cd ../../tmp (relative to current
location) - To go back, cd returns to previous directory
- To go up, cd .. go to parent directory
11Network and Mail
- telnet logs into remote system, telnet hostname
- finger find out info about user, finger yessi001
- ftp file transfer protocol, move files around
between remote systems - pine to read/send mail while logged in
12Editors
- pico editor similar to the pine mail editor
- Single-mode editor
- Always in input mode, special commands to move
text around etc - pico filename, CTRL-G for help
- Easy to learn about as complex as Window's
notepad - vi programmer's editor
- Two-mode editor
- Either in input mode or edit mode, must toggle
- vi filename, help via man page, type man vi
- Takes time to learn but has useful features
13Editors
- Emacs
- Lisp Editor
- Customizable (if you know lisp)
- Autoformating, syntax highlighting, command
completion, debugger, keyboard macros, etc - Most programmers prefer Emacs or vi
- My preference is vi, largely because I have
experience with it
14vi Basics
- Two Modes
- Command Mode
- For entering commands
- i, a, o, I, A, O, r, /ltsearchstrgt, ?ltsearchstrgt
- yy, p, P, ., q!, w, wq
- cw, dw, dd, D
- And many more
- Insert Mode
- For entering text
- esc returns to Command Mode
15vi commands
- i, a, o, I, A, O, r, R
- Various methods of entering insert mode
- i, a, o insert before/after/under curser
- I, A, O insert before/after/over line
- r, R replace char, type over
- /ltsearchstrgt, ?ltsearchstrgt
- Searching forward and backwards
- Use n to repeat search (find next)
16vi commands
- yy
- Yank (copy) n lines of text
- p, P
- Push yanked lines of text below/above
- . (Dot)
- Repeat last command (very useful)
- q!, w, wq
- Quit with saving (sometimes useful)
- Write without exiting
- Write and exit
17vi commands
- cw
- Change N() words
- dw
- Delete N words
- dd
- Delete N lines
- D
- Delete rest of line
18Compilers
- CC Sun C compiler
- g GNU C compiler
- We will use the GNU C compiler
- Create a source file (pico or vi), extension of
.cpp - g filename to compile the file
- ./a.out to execute the file (executable in a.out)
- g -o executablename filename
- (if you dislike a.out)
19Class Exercises
- Write hello world using the GNU C compiler
- Write a program (using GNU C) that declares a
template class for a dynamic linked list and then
adds five integers to an integer instantiation of
the list
20Makefiles
- Need a good way to compile big projects
- Simple
- Easy-to-use
- Not require a lot of typing
- Unix world has makefiles
- A file that tells how to build executables from
code - Name of the file is Makefile (or makefile)
- To run this file, type make
- The command make finds Makefile and executes
the commands listed in it
21Understanding Makefiles
- To understand makefiles, must know how programs
are compiled into executables - Two-stage process
- Compilation
- Linking
- Compilation
- Check for syntax semantic errors
- Generate object code for your statements
- Linking
- Links your object code w/ other routines
(libraries)
22Consider the file hello.C
- Compilation
- Checks for syntax errors (missing semi-colon,
etc) - Checks for semantic errors (e.g. cout gtgt x)
- Constructs object code (something the machine can
run) for your source code - Linking
- Combines your object code with other object code
needed during execution - Object code for I/O routines, math routines, etc.
- Builds one executable program
23Compiling hello.C (continued)
- Source code (hello.C) to object code (hello.o) is
done during compilation - The g command normally compiles and links (all
in one command) - g -c hello.C produces object file, no linking
done - Can then link this file manually if desired
- Can use g command to compile together both .C
files and .o files (previously compiled)
24Makefiles and C code
- Sample Makefile
- Hellolttabgthello.o
- lttabgtg hello.o
- To build Hello (target) you need hello.o
- Makefile knows hello.o can be built from hello.C
- Once have hello.o, do the command shown to build
executable - If multiple targets given, Makefile does first one
- The make command knows how C programs are
compiled - Makefiles consist of two lines
- First line is a target list of dependencies
- Second line is commands needed to build target
- Makefiles use tab as separator (not spaces)
25Larger Makefiles
- alllttabgttest1 test2 test3 test4
- test1lttabgtmain.o strategy1.o myStrategy.o
- lttabgtg -o test1 main.o strategy1.o myStrategy.o
- test2lttabgtmain.o strategy2.o myStrategy.o
- lttabgtg -o test2 main.o strategy2.o myStrategy.o
- test3lttabgtmain.o strategy3.o myStrategy.o
- lttabgtg -o test3 main.o strategy3.o myStrategy.o
- (one more stanza omitted for space reasons)
26Class Exercises
- Using the Unix system, write a sample Makefile.
- Non-template code from project1 is a multifile
project. My source files for these can be found
on the web at bama.ua.edu/yessi001
27Moving files to/from Unix
- Moving files between a PC and Unix
- Use ftp(file transfer protocol)
- PC must be connected to the network
- C\gtftp bama.ua.edu
- Connected to bama.ua.edu.
- 220-
- 220- Use of this system (lines suppressed here)
- 220 bama.ua.edu FTP server .
- User (bama.ua.edu(none)) cordes
- 331 Password required for cordes.
- Password ltenter password heregt
- 230 User cordes logged in.
- ftpgt put hello.C
- 200 PORT command successful.
- 150 Opening ASCII mode data connection for
hello.C. - 226 Transfer complete.
- ftp 85 bytes sent in 0.00Seconds
85000.00Kbytes/sec. - ftpgt quit
28Moving files to/from Unix (cont)
- Other ftp commands
- dir, lists all the files on the remote system
- cd xxx, changes directory on the remote
system(also have lcd to change local directory) - get xxx, gets a file from the remote system
- binary, toggles to binary mode (non-ASCII files)
- mput .cpp, copies all your cpp files to remote
sys(also have mget to retrieve files) - help, gives list of commands
29Windows version
- Most pcs have a windows version of ftp
- Start
- Programs
- WS_FTP (usually)
- WS_FPT
- Beside host name/Address type bama.ua.edu
- Enter your ID and password in appropriate spots
- Click OK
- This is a much easier way to ftp, however, be
familiar with Unix version as well
30Class Exercises (short)
- Find a C file on your local PC
- Copy it to your account on bama.ua.edu
- Copy a file from your bama account to your local
PC
31Unix File Permissions
- Each file in Unix has three levels of security
- What the user can do to it
- What people in the users group can do to it
- What everyone else can do with it
- The ls command can display security
- /fs/cordes ls -l
- total 10
- drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
cs325 - drwx------ 2 cordes facstaff 512 Aug 30 0510
foo - -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - drwx------ 2 cordes facstaff 512 Aug 30 0937
mail - drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
networks - ls l shows files and directories and gives extra
information such as permissions, userid, group,
last modified date
32Unix File Permissions (cont)
- /fs/cordes ls -l
- total 10
- drwxr-xr-x 2 cordes facstaff 512 Aug 30 1050
cs325 - drwx------ 2 cordes facstaff 512 Aug 30 0510
foo - -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - drwx------ 2 cordes facstaff 512 Aug 30 0937
mail - drwxr-xr-x 2 cordes facstaff 024 Mar 21 1999
networks
permissions user group last-mod-date filename
33Unix File Permissions (cont)
- -rw-r--r-- 1 cordes facstaff 81 Sep 3 0820
hello.C - - r w r - - r - -
- indicates this is a regular file, not a
directory - r w indicates the user can read write this
file - r indicates the group can read this file
- r indicates that others can read this file
34Changing Permissions
- chmod ltpermissionsgt filename
- Permissions consist of who /- what
- Who is u (user), g (group), o (others)
- What is r (read), w (write), x (execute)
- gives permission
- - takes permission
- Examples
- chmod u-w hello.C (user cannot write)
- chmod gor hello.C (group others can read)
- chmod ar hello.C (a is short for u, g, o)
- man chmod talks about changing permissions in
terms of octal numbers
35Class Exercises
- Type ls l in your home directory. Explain
what permissions are set for each file. - The file .plan in your home directory can tell
information about you (what you do). In order
for others to read this file, they need to be
able to execute your home directory (.) and also
read your .plan file. Set it up so that other
people can finger userid and see your .plan.
36Unix requirements for 325
- Basic manipulation of your files on Unix
- Create sub-directories for organization, etc
- Edit and compile C files
- Some projects are done on Unix
- Move files between PCs and Unix
37Unix Directories
- mkdir
- Creates directory
- rmdir
- Removes directory (must be empty)
38Unix apropos
- apropos keyword
- Searches on keyword for relevant info
- Useful when you cannot use man (looking for
command)
39Class Exercises
- Teams new to Unix
- Editors how do you
- Copy a line
- Delete a line
- Give the command to
- Rename hello.cpp to hello.C
- Copy (to your home dir) hello.cpp from
/st3/yessi001/cs325 - Mail hello.cpp to yourself
- Teams that know Unix
- Give a one-line command (you can use pipes) to
- Find the home directory of the user cordes
- Print your last name in hex (and octal)
- Print the first name of all the users on the
system with the last name Smith - Convert the contents of hello.C to uppercase