1 / 34

C151 Multi-User Operating Systems

C151 Multi-User Operating Systems. Emacs, Compilation, and Makefile. emacs Preliminaries. Emacs is a text editor Strength of emacs: command keys Meta Key Under Linux or Windows/putty: the Alt key Under MacOs it should be the Command key.

moroney
Download Presentation

C151 Multi-User Operating Systems

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. C151 Multi-User Operating Systems Emacs, Compilation, and Makefile

  2. emacs Preliminaries • Emacs is a text editor • Strength of emacs: command keys • Meta Key • Under Linux or Windows/putty: the Alt key • Under MacOs it should be the Command key. • In the emacs documentation, key sequences described as: • C-e – This is [Ctrl-e]. • C-x C-b – This is [Ctrl-x][Ctrl-b]. • C-x b – This is [Ctrl-x]b. • M-e – This is [Meta-e].

  3. emacs Preliminaries • Start emacs: • emacs –nw file_name • Same as new files and existing files • Editing text and exectuting commands can be done at the same time • Quit emacs • [Ctrl-x][Ctrl-c] • Cancel a half-typed command or stop a running command • [Ctrl-g]

  4. Inserting and Replacing Text • The default input mode is the insert mode. To switch to the overwrite mode, press the [Insert]key. • To save a file, use [Ctrl-x] [Ctrl-s]. • To save a file as a different filename, use [Ctrl-x][Ctrl-w].

  5. Navigation • emacs uses the control keys to move in the four directions. • [Ctrl-b] – move left, [Ctrl-f] – move right, [Ctrl-p] – move up, [Ctrl-n] – move down. • To scroll full page forward, use [Ctrl-v]. To scroll full page backward, use [Alt-v].

  6. Navigation • To move to the beginning of a line, use [Ctrl-a]. To move the end of a line, use [Ctrl-e]. • To move the beginning of the word, use [Alt-f]. To move the end of the word, use [Alt-b]. • To move to the beginning of the file, use [Alt-<]. To move to the end of the file, use [Alt->].

  7. Compiling C++ Programs with g++ • The g++ gnu compiler is for compiling and linking C++ programs. gcc is the C compiler/linker. • Compiling just one program: g++ source_file.cppBy default it produces an executable called a.out. • Some options for the g++:-oexecutable_name    defines the name of the result-c                               compilation only: creates an object-lname         links a library to the executable-Lpath                        specifies a path into which g++ should look for libraries-Ipath                       specifies a path into which g++ should look for header files

  8. Compiling Programs with g++ • g++ -o result source_file.cc • Create an executable file called result

  9. Compiling Multiple Source Files (1) • Each file can be compiled separately with the option -c to produce an object (the translation into machine code of the source file without links to all the function calls). • All the files are linked together to create the executable. • Example: We have two source code files: file1.cpp and file2.cpp • g++ -c file1.cpp (create file1.o) • g++ -c file2.cpp (create file2.o) • g++ -o result file1.o file2.o (create an executable result) • And we need to remove intermediate object files • rm –r f *.o (delete file1.o and file2.o)

  10. Compiling Multiple Source Files (2) • The name of all the objects to be linked in an executable must be listed in the command. • All the libraries we need must be linked with -l followed by the library name minus the prefix "lib" and minus the extension. • Example: the math library is called libm.a. To link this library, use the command -lm. • Although it is not needed here because standard library is included in g++. However, for nonstandard library, we need to include it.

  11. How G++ Work • Previous example: • g++ -c file1.cpp (compile and create file1.o) • g++ -c file2.cpp (compile and create file2.o) • g++ -o result file1.o file2.o –lm (link and an executable result) • -lm is not needed here.

  12. How to save time • Use makefile • A makefile is a file (script) containing : • Project structure (files, dependencies) • Instructions for files creation • Note that the Makefile mechanism is not limited to C programs and Linux/Unix systems

  13. Makefiles • Makefiles are command files that we can use for easy compilation of programs. • A makefile is invoked with the command make. • This command looks for a file with the name Makefile (makefile)in the current directory and executes some commands based on its content. • If the makefile is called anything but Makefile (makefile), or if it's not in the working directory, use the -f option with its name:make –f Makefile1make –f ../C151/Makefile

  14. Line-Oriented • Makefile is a line-oriented file. • If a line is too long and you would like it to span across several lines for clarity and convenience, you may do so, but you will have to escape the end of line by “\” at the end of each such a line.

  15. Basic Makefile Format # Comments VAR=value(s) # Actually, it is a macro target: list of dependencies <tab> command1 to achieve target <tab> command2

  16. Example of a Makefile for previous programs result: file1.o file2.o g++ –o result file1.o file2.o file1.o: file1.cpp g++ –c file1.cpp file2.o: file2.cpp g++ –c file2.cpp

  17. result (exe) sum.o main.o main.cpp sum.h sum.cpp sum.h Project structure • Project structure specifies module dependencies • Example : • Program contains 3 files • main.cpp, sum.cpp, sum.h • sum.h is included in both .cpp files • Executable should be the file result

  18. makefile result: main.o sum.o g++ –o result main.o sum.o main.o: main.c sum.h g++ –c main.cpp sum.o: sum.c sum.h g++ –c sum.cpp

  19. Rule syntax main.o: main.cpp sum.h g++ –c main.cpp tab dependency action Rule

  20. Dependencies • The list of dependencies can be: • Filenames • Other target names • Variables • Separated by a space. • May be empty; means “build always”. • Before the target is built: • it’s checked whether it is up-to-date (in case of files) by comparing time stamp of the target of each dependency; if the target file does not exist, it’s automatically considered “old”. • If there are dependencies that are “newer” then the target, then the target is rebuild; else untouched.

  21. Make operation - example FileLast Modified result 10:03 main.o 09:56 sum.o 09:35 main.cpp 10:45 sum.cpp 09:14 sum.h 08:39

  22. Make operation - example • Operations performed: g++ –c main.cpp g++ –o result main.o sum.o • main.o should be recompiled (main.cpp is newer). • Consequently, main.o is newer than result and therefore result should be recreated (by re-linking).

  23. Variables(Macros) and wild card Examples of variables: • HOME = /home/courses/cop4530/spring02 • CPP = $(HOME)/cpp

  24. Using Variables and wild card CC = g++ OBJECTS = main.o sum.o result: $(OBJECTS) $(CC) –o $@ $(OBJECTS) %.o : %.cpp sum.h           $(CC) –c *.cpp • $@: file name of the target • The “%” is used to indicate a wild card. • This is not a good solution. Why?

  25. Other Makefile Options • make • Usually only create one executable (the first target) • Make all • Will look at target all and might create more than one executables • Target all can be placed anywhere • Make run • Will run the executable created • make clean • Will remove intermediate files

  26. Make all example 1 all: result1 result 2 result1: file1.o file2.o g++ –o result file1.o file2.o result2: file1.o file2.o g++ –o result file1.o file2.o file1.o: file1.cpp g++ –c file1.cpp file2.o: file2.cpp g++ –c file2.cpp Command: make all make ( can generate same result, why?)

  27. Make all example 2 result1: file1.o file2.o g++ –o result file1.o file2.o result2: file1.o file2.o g++ –o result file1.o file2.o all: result1 result 2 file1.o: file1.cpp g++ –c file1.cpp file2.o: file2.cpp g++ –c file2.cpp Command: make all make (result will be different, why?)

  28. Remove intermediate files result: file1.o file2.o g++ –o result file1.o file2.o file1.o: file1.cpp g++ –c file1.cpp file2.o: file2.cpp g++ –c file2.cpp clean: rm –rf *.o Command: make clean

  29. Remove intermediate files: Using variables OBJS = main.o sum.o result: main.o sum.o gcc –o result main.o sum.o main.o: main.c sum.h gcc –c main.c sum.o: sum.c sum.h gcc –c sum.c clean: rm –rf $(OBJS) Command: make clean

  30. Run the program OBJS = main.o sum.o result: main.o sum.o gcc –o result main.o sum.o main.o: main.c sum.h gcc –c main.c sum.o: sum.c sum.h gcc –c sum.c clean: rm –rf $(OBJS) run: result Command (after command make): make run

  31. Example • Suppose that we want to compile a program called rover and that it is composed of the following 3 files:wheels.cc including wheels.hrover.cc including wheels.h and rover.hterrain.cc including terrain.hmain.cc including rover.h and terrain.h • Suppose that the executable also needs to link to a special library called libmars that can be found in /usr/lib/planets/

  32. Example (Makefile) rover: wheels.o rover.o terrain.o main.o g++ wheels.o rover.o terrain.o main.o -lmars -L/usr/lib/planets -o rover wheels.o: wheels.cc wheels.h g++ -c wheels.cc rover.o: rover.cc wheels.h rover.h g++ -c rover.cc terrain.o: terrain.cc terrain.h g++ -c terrain.cc main.o: main.cc rover.h terrain.h g++ -c main.cc clean: rm rover *.o

  33. Example (using variables) objects = wheels.o rover.o terrain.o main.o libs = -lmars libdir = -L/usr/lib/planets rover: $(objects) g++ $(objects) $(libs) $(libdir) -o rover rover.o: rover.cc wheels.h rover.h g++ -c rover.cc terrain.o: terrain.cc terrain.h g++ -c terrain.cc main.o: main.cc rover.h terrain.h g++ -c main.cc clean: rm rover $(objects)

  34. Reading Assignment • Textbook: Chapter 7 • http://www.eng.hawaii.edu/Tutor/Make/index.html

More Related