old chapter 10: programming tools a developer’s candy store
TRANSCRIPT
Old Chapter 10:Programming ToolsA Developer’s Candy Store
In this chapter …• Overview
• Shared Libraries
• make
• System calls
• CVS
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
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
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)
Compiling and Linking
Preprocessor
Compiler
Assembler
Linker
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>
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
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
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
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
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
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
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
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
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*
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
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)
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
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
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
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
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
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
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
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)
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
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
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