Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix Programming Environment

Similar presentations


Presentation on theme: "Unix Programming Environment"— Presentation transcript:

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

2 Editors which vi 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

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

4 Other Popular Editors Sublime Notepad++ Pspad
Some of them work on Windows

5 Compiler C++11 g++ Examples g++ -std=c++0x example.cpp
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++0x example.cpp g++ -std=c++11 example.cpp // since g++ 4.7

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

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>

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

9 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

10 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 Make is a project management utility. It automatically determines which files (or targets) need to be updated and how they should be updated. Note that make has very broad usages. Here we only focus on how to manage the development of C projects. Moreover, there are different versions of make. Our discussions are based on GNU make. Make relies on makefiles to tell it what to do. If you do not specify a makefile when you run make, the make will look for the default makefile names, which are GNUmakefile, makefile, and Makefile, in that order. You should not use GNUmakefile, which is only understood by GNU make. Other versions of make will not recognize the file. Makefile is recommended, as it appears at the beginning of the file list under a directory. You can overwrite the default makefile using the –f option. You can use any filename.

11 Make Explicit rules: Example: Target [target…] : [prerequisite…]
<tab> command Example: a.out : myprog1.c myprog2.c myprog3.c $(CC) myprog1.c myprog2.c myprog3.c An explicit rule informs make how to remake (update) one or more files (targets). It also specifies the dependencies or pre-requisites. That is, a target depends on these files, if any of them is changed, the target needs to be remade. The commands inform make how to remake (update) the targets if the dependencies have been modified since last remake. Note that, you must have a tab character at the beginning of the command line. Note also that multiple commands can be specified.

12 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 make will search an implicit rule to make foo.o and bar.o since we did not specify how to make them foo : foo.o bar.o cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS) Make supports many implicit rules. They are similar to the explicit rules, but you do not need to write them. Make understands and uses them when no explicit rules can be found. For example, there are standard ways to compile C program (.c file) into object file (.o file), and make supports such an implicit rule. For example, consider the following makefile (which is all contained in the makefile). Note that we did not specify how to remake foo.o and bar.o. make in this case will use the implicit rule to compile source code .c files to generate .o object files. You can add new implicit rules by defining pattern rules. Pattern rules are similar to explicit rules, except that, the target name contains the percentage sign (%), which can match any substrings.

13 Make Variable definitions: Some automatic variables
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 A variable definition assigns a text string to a variable. For example, in variable definition, CC=gcc, we assign gcc to CC. In this way, we can use CC in later part of the makefile. GNU make supports a number of predefined variables (names of programs), including CC, CXX etc. A number of additional variables used as arguments for other variables are also pre-defined. The default values for all of these is the empty string, unless otherwise noted. CFLAGS Extra flags to give to the C compiler. CXXFLAGS Extra flags to give to the C++ compiler. CPPFLAGS Extra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers). LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker, `ld'.

14 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

15 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 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 * 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

17 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 Log into a remote machine ssh For example: ssh 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.


Download ppt "Unix Programming Environment"

Similar presentations


Ads by Google