00:00

Unix Programming Environment: Editors, Compiler, Debuggers, Make, I/O Redirection, Project Submission, Tar Files, Useful

This guide covers essential tools and techniques for Unix programming, including editors like vi and emacs, compilers like g++, debuggers like gdb, and using make for building projects. It also discusses I/O redirection, project submission practices, working with tar files, and useful Unix commands.

Download Presentation

Unix Programming Environment: Editors, Compiler, Debuggers, Make, I/O Redirection, Project Submission, Tar Files, Useful

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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 ctrl-c to comment, etc) • See r1/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

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

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

  6. 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

  7. 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

  8. 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 r1/example2.cpp – To compile: make example2.x 8

  9. 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 r1/example2.cpp • • • • • • 9

  10. 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 r1/example2.cpp • • • • • Read gdb users manual to get more information and commands • 10

  11. 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

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

  13. 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 proj1.x : proj1.h proj1.o g++ -std=c++11 -o proj1.x proj1.o – make will search an implicit rule to make proj1.o since we did not specify how to create 13

  14. Make • Variable definitions: – String1 = string2 – E.g. CC=gcc CFLAG=-Wall –ansi –pedantic • Some automatic variables – $@: target file – $<: name of first prerequisite – $?: name of all prerequisites newer than target – $^: name of all prerequisites • See makefile1 and makefile2 • See makefile – Which can be used to compile any simple programs, e.g. – make example1.x – make example2.x 14

  15. make • If you have multiple targets (need to create multiple executables), you can add one target that depends on the other targets, for example all: proj2.x test_list.x proj2.x: list of dependents commands test_list.x: list of dependents commands 15

  16. 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 – cmd1 < file1 – Which redirects file1 to be the input to cmd1 – 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 – cmd2 > file2 • And they can be combined – cmd3 < file1 > file2 • You can find more information at – http://www.tldp.org/LDP/intro-linux/html/chap_05.html 16

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

  18. 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 proj1 directory) To tar all files – tar –cvf proj1.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 proj1.tar) To check the contents in the tar file – tar –tvf proj1.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 proj1.tar To reduce tar file size, please do not include object or executable files in your tar file • • • • • • 18

  19. 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 19

  20. Some Useful Unix Commands • Manual page of any command – man command • Log into a remote machine – ssh username@remote_machine – For example: ssh duan@linprog.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 proj2.x (to add executable permission) – If you download an executable program but it does not run, you should check the permission first. 20

  21. Some Useful Unix Commands • If you see lots of error messages when you compile a program, you can use pipe to send them to a command like less, for example – make |& less – g++ … |& less – In this way, you can see the errors page by page • You can use Unix history to run a previous command/program, for example – !g++ (exclamation then g++) – This will look for the last g++ command line to run 21

More Related