CISC181h 02/07/06 Covering basics of C++ compiling on strauss. Basic C program: First a slight digression about making files available on the web: The Unix command to list a directory is "ls -l" For example: > ls -l total 5 -rwxr-xr-x 1 pconrad 0376 3633 Feb 7 09:01 000.SampleLectureTopics.htm drwx------ 2 pconrad 0376 512 Feb 7 10:00 02.07 > The second line starts with drwx------ The d means that 02.07 is a directory The rwx means that the owner of file, pconrad, has read, write, execute permission. For a DIRECTORY, execute has a particular peculiar meaning... It means that if you already KNOW the name of a file in that directory, you are allowed to access it... but you may not LIST the files in that directory. If we want a directory to be available on the web, we have to open up read and execute permission to "others". We do that with the last three letters in the permission string We want: drwxr-xr-x The last three letters need to be r-x (The middle three have to do with "group" permissions--I'll invite you to read in your Andersen text Chapters 6,7,8 about those.) To make files available on the web, we use the command chmod a+rx filename To make an entire directory available, we use the command chmod -R a+rx directoryName The -R stands for "recursive". If you have a directory on the web that has permissions like: drwx------ And you try to pull it up, you'll get this message: Forbidden You don't have permission to access /CIS/181h/pconrad/06S/lect/02.07/ on this server. (or something like that... ) To fix it, we do the following command: chmod -R a+rx 02.07 For example: > chmod -R a+rx 02.07 > ls 000.SampleLectureTopics.htm 02.07 > ls -l total 5 -rwxr-xr-x 1 pconrad 0376 3633 Feb 7 09:01 000.SampleLectureTopics.htm drwxr-xr-x 2 pconrad 0376 512 Feb 7 10:07 02.07 > Now the lecture notes should be available on the web. Some emacs tricks we already covered: Splitting the screen with CTRL/X 2 (splits screen horizontally) CTRL/X 3 splits the screen vertically If you do CTRL/X 1 you get back to one window CTRL/X O (letter Oh) moves between/among the windows CTRL/X 0 (number zero) zeros out the current window More clever emacs tricks... To "include a file" in an emacs buffer (that is, suck the contents into the buffer at the current position).... Use CTRL/X I will "insert" or "include" a file The "squiggle" (properly called the tilde) i.e. ~ stands for your "home directory" under Unix. [Note: only the Unix shell and certain programs "know about" ~. In a C or C++ program, if you want to open a file, you might find that ~ doesn't work... we'll come back to that another time.] The sample C program from today's lecture... /* helloWorld.c P.Conrad A C program 02/07/2006 */ #include int main(void) { printf("Hello, World!\n"); return 0; } The sample C++ program from today's lecture // helloWorld.cc P. Conrad A sample C++ Program 02/07/2006 /* This is also a comment that takes up multiple lines */ #include using namespace std; int main(void) { cout << "Hello, World!" << endl; return 0; } How to compile this thing?!?!?! > ls helloWorld.c helloWorld.cc > cc helloWorld.c > ls a.out helloWorld.c helloWorld.cc > a.out a.out: Command not found. > If the "current directory" is not in our path, then we have to type ./a.out to tell the Operating System where to look for a.out. Otherwise, it searches through all the directories in our "PATH", and if it doesn't find it, we get "command not found". "echo $PATH" tells us our current path "which cc" tells us where in the path "cc" is coming from. > echo $PATH /opt/sfw/bin:/www/htdocs/CIS/software/local/bin:/www/htdocs/CIS/software/gnu/bin:/opt/sfw/bin:/www/htdocs/CIS/software/dist/apache-ant-1.6.5/bin:/usr/jdk1.5/bin:/home/usra/d9/55560/bin:/opt/bin:/opt/SUNWspro/bin:/usr/openwin/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/opt/X11R5/bin > which cc /opt/SUNWspro/bin/cc > which gcc /opt/sfw/bin/gcc > To compile a C++ program: > CC helloWorld.cc > ./a.out Hello, World! (from C++) > That uses the "SUN" compiler... CC is a commercially produced compiler. There is also a GNU compiler for C++. g++ is the name for the GNU C++ compiler. Which one should you use? You should probably switch back and forth between compilers often. Why? (1) Correct programs will typically produce the same results no matter which compiler you use. Programs with obscure bugs will often produce DIFFERENT outputs for different compilers. Example: Side comments: ANSI is the body that makes standards for many things, including C++. If you see a reference to ANSI C or ANSI C++, it means the "standard way" of writing and interpreting C or C++.