Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations."— Presentation transcript:

1 © Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations Compiling, Linking, and Running Your Program

2 © Janice Regan, CMPT 102, Sept. 2006 1 Using Basic LINUX Commands  To develop your programs you must first learn how to accomplish simple tasks within the LINUX operating system  Creating/Deleting and Editing files  Moving from directory to directory, and finding files  Organizing files, making your own directories  Compiling your program files  Interpreting and correcting syntax and semantic errors  Linking your programs  Running your programs  Debugging (finding and correcting all types of errors in your programs)

3 © Janice Regan, CMPT 102, Sept. 2006 2 Some basic commands  LINIX IS CASE SENSITIVE  Rectangle is NOT the same as RECTANGLE  passwd  Executed from your command line  Will ask you to input your old password and your new passwd twice  You can only change your own passwd  man commandname or info commandname  Tell me how to use the command, commandname  man –k string  Give me a l ist of commands whose descriptions include string  whoami  Tell me my username

4 © Janice Regan, CMPT 102, Sept. 2006 3 Directory structure  cd change directory  In myhome  cd Dir2 (goes to Dir2, the child directory of myhome)  cd Dir3/Dir5 (goes to Dir5 )  In Dir5  cd.. (goes to Dir3, the parent directory of Dir5)  cd../.. Or cd (goes to myhome)  cd../Dir6 (goes to Dir6)  Anywhere cd will take you to myhome (your home directory)  When you log in you will find yourself in your home directory Dir1 Dir5Dir6Dir7 myhome Dir2Dir3Dir4

5 © Janice Regan, CMPT 102, Sept. 2006 4 Using the directory structure  When you log in to begin an assignment, you will find yourself in your home directory  If your computer id is jones, then the name of your home directory will be jones (/home/jones)  You should make yourself a directory for the assignment, say ass1  To make a directory ass1 in directory jones cd mkdir ass1  Move into your new directory before starting your assignment cd ass1

6 © Janice Regan, CMPT 102, Sept. 2006 5 Beginning to write your program  You are now in a subdirectory that you created for your assignment  Next you need to create the file into which you will put your program  To created the file use a text editor. If the text editor is kate then  kate prog1.c Will create a file called prog1.c, in directory ass1 into which you will put your C program prog1. If the file prog1.c already exists it will be opened and ready to be modified.

7 © Janice Regan, CMPT 102, Sept. 2006 6 Moving, Renaming or Deleting Files  mv move file used to move or rename a file  mv a b file previously named a is now named b  mv a../a file a is moved from the present directory to the parent directory of the present directory  mv a d/a file a is moved from the present directory to the child directory a of the child directory d  rm remove fileused to delete a file  rm a1 deletes a file, a1, from the current directory  rm../a1 removes a file, a1, from the parent of the present directory  rm d/a1 removes a file, a1, from the child directory a of the child directory d

8 © Janice Regan, CMPT 102, Sept. 2006 7 Where am I? What is here? Is it in this file?  pwdWhat is the name of the directory I am in  ls give me a list of all the files in this directory  ls –al give me a list with more information about each file  grep pattern filepaths  Find all occurrences of pattern in requested files

9 © Janice Regan, CMPT 102, Sept. 2006 8 Copying, viewing, and combining Files  more filename or less filename  Displays contents of file filename one page (screen) at a time  cp filename1 filename2  Make a copy of filename1 with name filename2  cat filename1 filename2 filename3  Will print the contents of each file in sequence Contents of filename1 Contents of filename2 Contents of filename3

10 © Janice Regan, CMPT 102, Sept. 2006 9 Wildcards  Wildcards are used to represent multiple possibilities  * matches any number of characters  ? Matches a single character  Examples  ls a? List all files in the current directory beginning with a and having a name of length 2 characters.  grep mystring */*file Find all occurences of the string “mystring” in all files whose names end with the string “file”. The files ending with string “file” must be in a subdirectory of the present directory  rm [a-c]* Remove all files with filenames beginning with a b or c from the current directory

11 © Janice Regan, CMPT 102, Sept. 2006 10 Redirection  Redirect input and/or output  > filename redirect standard output from screen to file. cat file1 file2 file3 > f4 A new file f4 is opened and the contents of file1, file2 and file 3 are successively added to the file f4. If f4 exists it will be overwritten.  < filename take input from file filename a.out < datafile (a.out is the default output file for a compiled executable)

12 © Janice Regan, CMPT 102, Sept. 2006 11 Redirection and Piping  Redirection  >> filename redirect standard output from screen to file. Appends output to an existing file cat file1 file2 file3 >> f4 The contents of file1 then file2 then file3 will be successively appended to the current contents of f4  Piping allows you to send the output of one process to become the input of another process without using io to store it in an intermediate file  ls a* | more List the files in the current directory whose names start with a, one page at a time

13 © Janice Regan, CMPT 102, Sept. 2006 12 Creating, Editing and Deleting Program Files  For program files and data files you must use a text editor not a word processor  In Windows Word is a word processor. The file it saves includes information in addition to the text that appears on your page. The information is stored in a file with a special format that is readable only by the word processor application.  In windows Notepad is a text editor it saves only the text you type in. The text is stored as simple characters and is readable by many applications including compilers  In Linux the suggested text editor is called kate. If you are already familiar with LINUX you may use your favorite text editor

14 © Janice Regan, CMPT 102, Sept. 2006 13 Writing your program  Log in to a Linux machine (or a Cygwin window if you are working at home)  The red hat on the bar at the bottom of the screen is the icon for your start menu.  Select system tools from your start menu  Select terminal from the system tools menu  A terminal window like the one below will appear  In the terminal window type kate, followed by enter  A kate text editor window should open for you.

15 © Janice Regan, CMPT 102, Sept. 2006 14 Entering your program into a file  Either  Enter the command kate followed by enter, to open kate  Type your program into the blank kate text editor window  Save your program to a file with a name ending in.c  Or  Open you program file with the command kate programname.c  Select Open from the file menu  Browse to find the existing file you wish to edit  Select the file to edit, then make desired changes to the contents of the file Each time you make changes to your program save those changes by selecting save in the file menu in kate. After saving reload by selecting reload from the kate file menu

16 © Janice Regan, CMPT 102, Sept. 2006 15 Next step, compile the program  Compile your program  Make sure your most recent changes have been saved, and that the saved file has been reloaded into kate.  Press the terminal icon at the bottom of the kate window. This will produce a split screen, the upper window will contain you code in the kate editor, the lower half is a terminal window in which you can type in the commands discussed below.  To compile the program in the text file filename.c execute the following command by typing it into the kate terminal window (remember to type the enter key after the end of the command) gcc –Wall –c –o programname.o programname.c

17 © Janice Regan, CMPT 102, Sept. 2006 16 Basics of compiling with gcc  gcc –Wall –c –o programname.o programname.c  gcc is the name of the c compiler application we are using (GNU C Compiler or GCC). There are many options available when using this compiler, we will cover only a few of these options in this course, You can see explanations of these and other options at any time by typing man gcc.  -Wall is an option that provides more extensive compiler messages warning you of potential problems in your code. These extra warning messages will help you find the problems in your code.  -c and option that tells the c compiler to compile the input text file only, the linker will not be executed.

18 © Janice Regan, CMPT 102, Sept. 2006 17 Basics of compiling with gcc  gcc –Wall –c –o programname.o programname.c  -o filename.o specifies that the name of the object file which will contain your compiled code. In the case of the example above this name would be expansion.o. Regardless of the name you use for your object file it should end in.o to tell the compiler it is an object file resulting from compiling a c text file.  filename.c The test file containing the C code you wish to compile. The name of this file must end with.c. Ending the file with.c tells the compiler that the text file contains C code, rather than objective C, C++, or other flavors of C code that this compiler also supports.

19 © Janice Regan, CMPT 102, Sept. 2006 18 After trying to compile …  When you compile your code using a variant of the command above (filenames will change) you will observe one of two outcomes  You will see a prompt just like the one at which you typed in the command above. This means that your C code has been successfully compiled, and an object file containing the compiled code has been created

20 © Janice Regan, CMPT 102, Sept. 2006 19 After trying to compile …  You will see a list of error messages that tell you there are still syntax and semantic errors within your C code. In this case no object file is created, (Be careful, if you have an object file from a previous version of your code, linking the object file after a failed compile will mean you are recreating the previous version not the present version with your most recent changes). Your next task is to correct those compile time errors and recompile the text file. Remember to save and reload your file after each group of changes you make before you recompile.

21 © Janice Regan, CMPT 102, Sept. 2006 20 Linking your program  When you can compile your code successfully try linking your code  To link the object file expansion.o execute the following command by typing it into the kate terminal window (remember to type the enter key after the end of the command)  gcc programname.o -lm -o programname

22 © Janice Regan, CMPT 102, Sept. 2006 21 Linking your program  gcc programname.o -lm -o programname  expansion.o specifies that the name of the object file which contains your compiled code. In the case of the example above this name would be expansion.o. Regardless of the name you use for your object file it should end in.o to tell the compiler it is an object file resulting from compiling a c text file.  -l specifies that a library should be linked with your input object code to produce your output executable code. In the example above –lm indicates that the standard math library should be linked with you own object code. You will need the standard math library is you use any functions like sin, cos, exp (exponential) etc.

23 © Janice Regan, CMPT 102, Sept. 2006 22 Linking your program  gcc programname.o -lm -o programname  -o expansion specifies that the name of the executable file which will contain the compiled and linked code. In the case of the exampleabove this name would be expansion. It is not necessary to give your executable file any particular extension (like.exe) to identify them

24 © Janice Regan, CMPT 102, Sept. 2006 23  You can also compile and link your code with a single command (shown below). In this case no object code will be created, you will create an executable file directly from your C text file.  gcc -Wall -lm -o programname programname.c

25 © Janice Regan, CMPT 102, Sept. 2006 24 Running and debugging your code  To run your executable code from the terminal window execute the command below by typing it and then pressing the enter key./programfile  One of three outcomes will happen when you run your program  Your program may work correctly and print the expected results OR  Your program may terminate with and error OR  You program may complete and produce an incorrect answer  In the second two cases you will need to correct your logic errors. After correcting your errors you will need to save your changes, reload the file into kate, recompile and relink before you can retest your new executable file


Download ppt "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Preparation for working in the CSIL Basic LINUX operations."

Similar presentations


Ads by Google