Unix Programming Environment Editors Compiler Debuggers Make Unix

  • Slides: 19
Download presentation
Unix Programming Environment • • Editors Compiler Debuggers Make Unix I/O redirection Project submission

Unix Programming Environment • • Editors Compiler Debuggers Make Unix I/O redirection Project submission Tar files Other useful Unix commands 1

Editors • We recommend two editors: vi and emacs – You should really retire

Editors • We recommend two editors: vi and emacs – You should really retire “pico” for editing programs • vi, classical editor on Unix/Linux – When we say “vi”, we mean “vim” (vi improved for programmers) – Normally vi has been linked to vim – Run the following command to check if your vi has been linked to vim (alias) which vi • Color syntax highlighting (for vim) – Edit your. vimrc file under your home directory and add the following line syntax on • Two modes: insert and command. Default mode is command – Entering insert mode: a, i, o or O 2

Emacs • Emacs configuration file –. emacs under your home directory – You can

Emacs • Emacs configuration file –. emacs under your home directory – You can shorten the commands by defining commands in your. emacs resource file (for example, meta-g to go a line, ctrl-c to comment, etc) • See r 1/emacs_example for an example configuration file – You can copy it to. emacs under your home directory if you want to use this resource file for emacs – This configuration supports color syntax highlight 3

Other Popular Editors • • • Sublime Notepad++ Pspad Clion Atom VSCode • Some

Other Popular Editors • • • Sublime Notepad++ Pspad Clion Atom VSCode • Some of them work on Windows 4

Compiler • g++ • Examples – – – g++ example. cpp g++ -c example.

Compiler • g++ • Examples – – – g++ example. cpp g++ -c example. cpp g++ -g example. cpp g++ -Wall example. cpp –lm • C++11 – g++ -std=c++0 x example. cpp – g++ -std=c++11 example. cpp // since g++ 4. 7 – Note that you will need to specify c++11 standard when you compile some of the assignments 5

Some Important Options of g++ • -Wall – This enables all the warnings about

Some Important Options of g++ • -Wall – This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. • -pendantic – Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. • See g++ manual for details • We recommend you to enable these two options when you compile your program 6

Header Files • If you have a header file (e. g. , example. h)

Header Files • If you have a header file (e. g. , example. h) in the same directory as the main program (e. g. , example. cpp), there are two ways for you to include the header file in the main program, using either “example. h”, or <example. h> • Double quote – Searching current directory for header file • Angle bracket – Searching system directory for header file – Which can be changed by the –I operation of compiler g++ – g++ -I. –o examplex example. cpp (example. cpp contains #include <example. h> 7

Debugger • The code must be compiled with –g option. • ddd, xxgdb, gdb

Debugger • The code must be compiled with –g option. • ddd, xxgdb, gdb • The power of a debugger: – Finding the line that causes coredump. – See example: • Break point/show value/change value/step/next/continue/print • Demo with r 1/example 2. cpp – To compile: make example 2. x 8

Debug Segmentation Fault with Core dump File • A file containing memory image of

Debug Segmentation Fault with Core dump File • A file containing memory image of process when it terminates (crashes) • A common reason of core dump file not created is resource limit on core file • Controlling core file size is shell specific – csh: • limit [resource] [limit], or simply unlimit to remove all limits • Where resource should be coredumpsize – sh: ulimit -c [limit] • Load core dump file into debugger – gdb: gdb executable_file coredump_file – ddd: ddd executable_file coredump_file • To locate the line that causes core dump – backtrace (or bt) • Demo with r 1/example 2. cpp 9

Debug Segmentation Fault w/o Using Core dump File • Load executable into debug –

Debug Segmentation Fault w/o Using Core dump File • Load executable into debug – gdb: gdb executable_file – ddd: ddd executable_file • Run program in debug – run (gdb command) • After you run the program, you will encounter segmentation fault, gdb will show some info regarding the error and line causing problem • You can also use a few gdb commands to locate problem – where – backtrace (or bt) • Demo with r 1/example 2. cpp • Read gdb users manual to get more information and commands 10

Make • A tool to update files that are derived from other files. Great

Make • A tool to update files that are derived from other files. Great for software development. • Discussions based on GNU make utility • make command – make [-f makefile][option][target] • Common make file names are Makefile, makefile, in that order • The default files can be overwrite with the –f option – make –f myprog. mk 11

Make • Explicit rules: – – Target [target…] : [prerequisite…] <tab> command … •

Make • Explicit rules: – – Target [target…] : [prerequisite…] <tab> command … • Example: – proj 1. x: proj 1. h proj 1. cpp g++ -std=c++11 proj 1. cpp –o proj 1. x 12

Make • Implicit rules: – Tell make how to use certain customary techniques to

Make • Implicit rules: – Tell make how to use certain customary techniques to remake a target so that you do not have to supply the rules – For example, look at the following makefile proj 1. x : proj 1. h proj 1. o g++ -std=c++11 -o proj 1. x proj 1. o – make will search an implicit rule to make proj 1. o since we did not specify how to create 13

Make • Variable definitions: – String 1 = string 2 – E. g. CC=gcc

Make • Variable definitions: – String 1 = string 2 – E. g. CC=gcc CFLAG=-Wall –ansi –pedantic • Some automatic variables – – [email protected]: target file $<: name of first prerequisite $? : name of all prerequisites newer than target $^: name of all prerequisites • See makefile 1 and makefile 2 • See makefile – Which can be used to compile any simple programs, e. g. – make example 1. x – make example 2. x 14

I/O Redirection • For a program (or command) that reads from the standard input

I/O Redirection • For a program (or command) that reads from the standard input (normally, the keyboard), we can redirect input so that the input is taken from a file, instead of standard input, for example – cmd 1 < file 1 – Which redirects file 1 to be the input to cmd 1 – We will frequently use input redirection to test a number of projects. For these, your program will read from standard input, and you do not need to worry about anything in your program regarding the input redirection. It is handled by Unix shells. • Similarly, we also have output redirection – cmd 2 > file 2 • And they can be combined – cmd 3 < file 1 > file 2 • You can find more information at – http: //www. tldp. org/LDP/intro-linux/html/chap_05. html 15

Project Submission • All projects will be submitted via Canvas assignment page • You

Project Submission • All projects will be submitted via Canvas assignment page • You can submit multiple times (otherwise, let us know) 16

Tar files • You need to compress all files (including makefile, header files, source

Tar files • You need to compress all files (including makefile, header files, source code files, etc) into a single file when you submit your projects • Some common tar usages (assuming all files you need to submit are under proj 1 directory) • To tar all files – tar –cvf proj 1. tar * – You need to be extremely careful, or you risk to overwrite an existing file (for example, some students in the past forgot to specify proj 1. tar) • To check the contents in the tar file – tar –tvf proj 1. tar • To untar a tar file to get all files in the tar file (in order to not overwrite existing files, you should do this in a different directory) – tar –xvf proj 1. tar • To reduce tar file size, please do not include object or executable files in your tar file 17

Submission Verification • The most reliable way to verify if you have submitted the

Submission Verification • The most reliable way to verify if you have submitted the correct version is to – – Download the submission to a new directory Untar the tar file, Check the source files Compile and run it to make sure it is correct • Contact the FSU/ITS team if you cannot download your submission. • Do not save and untar the submission file in the same directory of your working code – When you untar, it will overwrite your existing programs 18

Other Useful Unix Commands • Manual page of any command – man command •

Other Useful Unix Commands • Manual page of any command – man command • Log into a remote machine – ssh [email protected]_machine – For example: ssh [email protected] cs. fsu. edu • Retrieve a file or directory from a web server – wget URL • Create an empty file or change timestamp – touch filename • Change permission of a program – chmod mode filename – chmod u+x proj 2. x (to add executable permission) – If you download an executable program but it does not run, you should check the permission first. 19