old chapter 10: programming tools a developer’s candy store

29
Old Chapter 10: Programming Tools A Developer’s Candy Store

Upload: brent-may

Post on 31-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Old Chapter 10: Programming Tools A Developer’s Candy Store

Old Chapter 10:Programming ToolsA Developer’s Candy Store

Page 2: Old Chapter 10: Programming Tools A Developer’s Candy Store

In this chapter …• Overview

• Shared Libraries

• make

• System calls

• CVS

Page 3: Old Chapter 10: Programming Tools A Developer’s Candy Store

UNIX/Linux and Programming• Since the OS and most of the tools are

written in C, continued development necessitated a thorough set of C development tools

• Over time, many other languages found their way to UNIX/Linux, making it an excellent development platform

• Machine Independent + Portable + Standards Based = Great for Development

Page 4: Old Chapter 10: Programming Tools A Developer’s Candy Store

C Programming• Most prevalent C compiler available is the

GNU C Compiler, gcc

• gcc has progressed to support many other languages including C++, java, cobol, fortran

• Has many options and complex syntax, but most simply it compiles and links

Page 5: Old Chapter 10: Programming Tools A Developer’s Candy Store

C Programming• This is not a programming course, so we’re

not interested in how to program

• We’ll focus on the utilities and the process of building apps

• We’ll focus on C, but these processes extend to most other compiled languages

• Scripting languages are different – interpreted (ex. perl, python, shell scripts)

Page 6: Old Chapter 10: Programming Tools A Developer’s Candy Store

Compiling and Linking

Preprocessor

Compiler

Assembler

Linker

Page 7: Old Chapter 10: Programming Tools A Developer’s Candy Store

Preprocessor• Directives in C/C++ files

• #include include_file– Inserts code files from other locations– Can be files you’ve written, or standard library

files

• Ex:– #include “myHeader.h”– #include <stdio.h>

Page 8: Old Chapter 10: Programming Tools A Developer’s Candy Store

Compiler• Changes high level language code into

assembly code

• Here code is optimized by the compiler (there are varying levels of optimization)

• The better the compiler, the neater and more efficient this code is

Page 9: Old Chapter 10: Programming Tools A Developer’s Candy Store

Assembler• Assembly code is then turned into machine-

readable object code

• Each code file compiled and assembled creates a file with a .o extension

• These are binary files – not readable by human eye

Page 10: Old Chapter 10: Programming Tools A Developer’s Candy Store

Linker• The last step is to combine all the object

code into one executable

• Combines with system libraries with common function calls – system specific

• Executables are in Executable and Linking Format (ELF) – standardized

Page 11: Old Chapter 10: Programming Tools A Developer’s Candy Store

Shared Libraries• Most systems contain a collection of

standard shared libraries

• Contain standard defined functions, written for the specific OS and machine architecture

• Most often found in /lib, /usr/lib• If you’ve got a 64-bit system – also have /lib64 and /usr/lib64

• Also /usr/X11R6/lib for X Windows apps

Page 12: Old Chapter 10: Programming Tools A Developer’s Candy Store

Shared Libraries, con’t• Two types of shared libraries

• Dynamic – not linked when compiled, called upon execution

• Static – linked when compiled, any changes to library forces recompilation of program

• To see what libraries a program uses, use ldd

Page 13: Old Chapter 10: Programming Tools A Developer’s Candy Store

make• When you change a source file that is

referenced by other files, you have to recompile your program

• If your program has a great deal of source files, it can be hard to remember what is dependent on what

• Enter make

Page 14: Old Chapter 10: Programming Tools A Developer’s Candy Store

make con’t• make will only recompile files that it needs to

• Checks modification dates to see if any dependent files need to be recompiled

• Uses a file called a Makefile to keep track of the dependencies

• Saves time and is convenient

Page 15: Old Chapter 10: Programming Tools A Developer’s Candy Store

Makefile• Format:

target: prerequisite-list

construction-commands• First line lists files that are needed to create

target – if one changes, we need to recompile

• Second line MUST start with a tab, contains the command to compile/link/etc

Page 16: Old Chapter 10: Programming Tools A Developer’s Candy Store

Makefile exampleform: size.o length.ogcc –o form size.o length.o

size.o: size.c form.hgcc –c size.c

length.o: length.c form.hgcc –c length.c

form.h: num.h table.hcat num.h table.h > form.h

clean:rm *.o *core*

Page 17: Old Chapter 10: Programming Tools A Developer’s Candy Store

make con’t• When you run make without arguments, it

will attempt to build the first target in the file

• So the end result (complete program) should be the first line

• You can force make to build any target by issuing it as an argument– Ex: make clean

Page 18: Old Chapter 10: Programming Tools A Developer’s Candy Store

Advanced Makefiles• More complex Makefiles contain macros,

which are like variables

• Referenced via $(macro)

• CC – compiler

• CFLAGS – C compiler flags

• CPPFLAGS – C++ compiler flags

• COMPILE.c – translates to:

$(CC) –c $(CFLAGS) $(CPPFLAGS)

Page 19: Old Chapter 10: Programming Tools A Developer’s Candy Store

Advanced Makefiles con’t• LDFLAGS – linker flags

• LINK.c – translates to:

$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)

• FILES – list of source files

• HEADERS – list of header files

• OBJECTS – list of object files

Page 20: Old Chapter 10: Programming Tools A Developer’s Candy Store

Debugging• First level of debugging is simply putting

breaks and print statements in your code

• Can be time consuming

• To help prevent problems tell compiler to show warnings about common mistakes

• gcc –Wall will show all warnings

Page 21: Old Chapter 10: Programming Tools A Developer’s Candy Store

Debugging con’t• Bigger problems require the use of a

symbolic debugger

• Allows execution to be monitored and controlled

• Allows setting of break points and display of variable contents

• Lets you trace through memory dumps to see where run time errors occur

Page 22: Old Chapter 10: Programming Tools A Developer’s Candy Store

gdb• GNU gdb symbolic debugger

• Compile programs with –g to allow debugging

• This adds debugging information to program, a list of symbols that relate to variables and functions

• Also allows you to associate system calls in executable to lines in source file

Page 23: Old Chapter 10: Programming Tools A Developer’s Candy Store

gdb con’t• gdb is very robust and complex, lots of

commands and options

• Other graphic front ends are available, but at the command line your best bet is probably going to be gdb

Page 24: Old Chapter 10: Programming Tools A Developer’s Candy Store

System Calls• Kernel is responsible for process control,

filesystem management, and operation of peripheral devices

• We have access to these kernel abilities via system calls

• The system works directly on our behalf

• Depending on the operation you may need elevated privileges

Page 25: Old Chapter 10: Programming Tools A Developer’s Candy Store

Important system calls• fork() – create new process

• exec() – runs program in memory

• getpid() – get a process ID

• kill() – terminates a process

• open() – open a file

• read() – read an open file

• write() – write to an open file

Page 26: Old Chapter 10: Programming Tools A Developer’s Candy Store

CVS: Source Code Management

• When multiple people are working on a project, source code management becomes an issue

• Need something to keep track of revisions, and make sure users don’t step on one another

• Enter CVS – Concurrent Versions System– Developed from Revision Control System (RCS)

Page 27: Old Chapter 10: Programming Tools A Developer’s Candy Store

CVS• Allows users to check out source code,

modify it, the check it back in and integrate their changes

• When you check out files, a copy is made for you to edit

• Originals are kept in data store

• You then can commit your changes to the store

Page 28: Old Chapter 10: Programming Tools A Developer’s Candy Store

CVS con’t• Syntax:

cvs [cvs-options] command [options]

• Commands include:– checkout – get copy of source– commit – submit changes– update – check for changes made by others– add – add new file to project– delete – remove file from project

Page 29: Old Chapter 10: Programming Tools A Developer’s Candy Store

CVS con’t• A thorough treatment of CVS is beyond the

scope of this class

• Unless you are a programmer, you probably won’t come into contact with CVS

• If you are, you will learn to appreciate it when you work on group projects

• Other CVS alternatives: Subversion, BitKeeper, Git