Download presentation
Presentation is loading. Please wait.
Published byBrook Gregory Modified over 9 years ago
1
240-491 Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which may use libraries 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001 13. Large Programs
2
240-491 Adv. UNIX: large/132 Overview 1. Independent Compilation 2. Global Information 3. Header Files 4. Information Hiding 5. The make Utility 6. Libraries 7. A Static Library Example
3
240-491 Adv. UNIX: large/133 1. Independent Compilation v Split a large program into two files: : int main() {... x = maxi(a, b);... } : main.c : int maxi(int x,int y) {... return w; } : others.c
4
240-491 Adv. UNIX: large/134 Compilation and Linking v Compilation: $ gcc -c main.c /* produces main.o */ $ gcc -c others.c /* produces others.o */ v Linking: $ gcc -o foo main.o others.o /* creates foo */
5
240-491 Adv. UNIX: large/135 Why Split the Code into Files? v Advantages: –reduces the complexity of the complete program by organizing it into separate files –speeds up recompilation: u only recompile what’s needed u linking is faster than compilation continued
6
240-491 Adv. UNIX: large/136 v Disadvantages: –multiple object files use more storage space –compiling all the separate “.c” files takes longer than a single combined “.c” file
7
240-491 Adv. UNIX: large/137 2. Global Information extern variables –declarations of global variables used across multiple files v function prototypes –add to every file where the function is used v constants, types, macros –repeat in every file that uses them
8
240-491 Adv. UNIX: large/138 Example #include #define SIZE 10 int maxi(int x, int y) ; int flag; float f: int main() { int x, a, b: f = 2.0; x = maxi(a, b); while (x < SIZE) : } : main.c #include #define SIZE 10 int maxi(int x, int y) ; extern int flag; extern float f: int maxi(int x, int y) {... f = f + 1.0; while (x < SIZE)... return w; } : others.c
9
240-491 Adv. UNIX: large/139 Definition vs. Declaration v float f; int flag; –variable definition: name, type, and storage extern float f; extern int flag; –variable declaration: name, type v Each variable must have only one definition, but can have many declarations.
10
240-491 Adv. UNIX: large/1310 3. Header Files Use a “.h” file to put global information in a single place, and then use #include to include it in all the “.c” files. v Used to store: –constant definitions –type declarations, extern declarations –macros –function prototypes
11
240-491 Adv. UNIX: large/1311 Example #define SIZE 10 int maxi(int x, int y); extern int flag; extern float f: defs.h
12
240-491 Adv. UNIX: large/1312 Use #include #include “defs.h” int flag; float f: int main() { int x, a, b: f = 2.0; x = maxi(a, b); while (x < SIZE) : } : main.c #include #include “defs.h” int maxi(int x, int y) {... f = f + 1.0; while (x < SIZE)... return w; } : others.c
13
240-491 Adv. UNIX: large/1313 Compilation / Linking v As before: $ gcc -c main.c $ gcc -c others.c $ gcc -o foo main.o others.o defs.h must be accessible (i.e. in the same directory).
14
240-491 Adv. UNIX: large/1314 4. Information Hiding If a global variable or function definition is preceded by static then it can only be accessed by other things in its file. : static int table[SIZE]; : static int search(int key) {... } int maxi(int a, int b) {... } others.c
15
240-491 Adv. UNIX: large/1315 5. The make Utility Write down the compilation dependencies between files in a “ makefile ”. make uses the makefile to decide what to recompile when a file is changed: –make decides on recompilation, not you
16
240-491 Adv. UNIX: large/1316 5.1. The makefile Format target file: list of files --tab-- command line v e.g. foo: main.c others.c defs.h gcc -o foo main.c others.c v Use: make foo dependency line makefile
17
240-491 Adv. UNIX: large/1317 5.2. A more efficient makefile v Split compilation process into 3 stages: foo: main.o others.o gcc -o foo main.o others.o main.o: main.c defs.h gcc -c main.c others.o: others.c defs.h gcc -c others.c v The compiler will only execute those command lines where the dependent files have changed.
18
240-491 Adv. UNIX: large/1318 5.3. Using Built-in Rules make knows the connection between “.c”, “.o” and “.h” files –this means that the makefile can be shorter v foo: main.o others.o gcc -o foo main.o others.o main.o: defs.h others.o: defs.h or: main.o others.o: defs.h
19
240-491 Adv. UNIX: large/1319 Changing Rules make uses cc -cO by default Change by redefining the CC and CFLAG values at the start of makefile : CC = gcc CFLAGS = -g /* options become -cg */
20
240-491 Adv. UNIX: large/1320 5.4. Declaring Variables Create the variable OBJS : OBJS = main.o file1.o file2.o \ file3.o file4.o v Use: foo: $(OBJS) gcc -o foo $(OBJS) $(OBJS): defs.h
21
240-491 Adv. UNIX: large/1321 5.5. Other Uses of makefile Common addtions to makefile : : test: foo rm -f test.out foo test.in > test.out clean: rm -f $(OBJS) rm -f test.out all: foo test clean
22
240-491 Adv. UNIX: large/1322 Uses v make foo v make test v make clean v make all
23
240-491 Adv. UNIX: large/1323 5.6. Options to make OptionsMeaning -f Use a file other than makefile. -k Try to continue after a command has failed. -n Only print the commands. v e.g. make -f bm all make -k foo make -n all good for testing the makefile
24
240-491 Adv. UNIX: large/1324 5.7. Touching Files v touch file –touches the file, updating its modification date touch foo –updates foo ’s modification date –make foo will now cause foo to be regenerated since its modification date has changed
25
240-491 Adv. UNIX: large/1325 6. Libraries v A library is a collection of useful functions (e.g. for graphics manipulation) in a single file. v Linux supports two types of library: –static libraries (files end in '.a' ) –shared libraries (files end in '.so' ) continued
26
240-491 Adv. UNIX: large/1326 v When several programs use a static library, they each load a copy into memory –a copy is included inside your program when it is compiled –the object file can be quite large –a static library is sometimes called a archive continued
27
240-491 Adv. UNIX: large/1327 v When several programs use a shared library, only one copy is loaded into memory –it is loaded when the program is first run (if there isn't a copy already in memory) –less memory is used –Linux is moving over to shared libraries u based on the ELF binary format
28
240-491 Adv. UNIX: large/1328 6.1. The Linking Stage v Linking example: $ gcc -o foo main.o others.o v The linker does two main tasks: –1) matches the function calls in main.o, others.o to the function implementations in those files; –2) matches the function calls to the standard libraries (e.g. printf() ). continued
29
240-491 Adv. UNIX: large/1329 The compiler knows which libraries to use because of the “.h” files that you have included. The library function implementations are loaded automatically for the standard C library ( libc.so ) –other libraries must be specified on the gcc command line continued
30
240-491 Adv. UNIX: large/1330 v A library consists of object files –only the object files containing the functions used in your program are loaded –this means it is best to separate functions into many (small) object files inside a library –then less of the library will be loaded
31
240-491 Adv. UNIX: large/1331 6.2. Library Locations Standard libraries are in /lib and /usr/lib Other shared library directories are given in /etc/ld.so.conf –on calvin : $ cat /etc/ld.so.conf /usr/X11R6/lib/Xaw3d /usr/X11R6/lib /usr/lib/libc5-compat /lib/libc5-compat
32
240-491 Adv. UNIX: large/1332 6.3. Using Libraries Use the -l option of gcc to specify a library: $ gcc -o examp examp.c -lm gcc will try to use a shared library ( libm.so ); failing that it will try to use a static library ( libm.a ) continued
33
240-491 Adv. UNIX: large/1333 v Use a library's full name: $ gcc -o examp examp.c /usr/lib/libm.so v Use a library in a non-standard directory: $ gcc -o foobar -L /usr/foobar/lib fred.c -lfb the library must be supplied last, so all the functions are known
34
240-491 Adv. UNIX: large/1334 6.4. Comparing Static and Shared v /* sqrtest.c */ #include #include int main() { double x = 9.0; printf("%f\n", sqrt(x)); return 0; } continued
35
240-491 Adv. UNIX: large/1335 v gcc -o sqrtest sqrtest.c -lm $ sqrtest 3.000000 $ ldd sqrtest libm.so.6 => /lib/libm.so.6 (0x40019000) libc.so.6 => /lib/libc.so.6 (0x40036000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) $ ls -alt sqrtest -rwxrwxr-x 1 ad users 4964 Dec 1 14:40 sqrtest* continued ldd prints details on the shared libraries used.
36
240-491 Adv. UNIX: large/1336 v $ gcc -o sqrtest sqrtest.c -static -lm $ sqrtest 3.000000 $ ldd sqrtest statically linked (ELF) $ ls -alt sqrtest -rwxrwxr-x 1 ad users 246373 Dec 1 14:40 sqrtest* bigger since the libraries are added in at compile time. use static libraries
37
240-491 Adv. UNIX: large/1337 6.5. Shared Library Links When a programmer specifies a shared library (e.g. libm.so ), it is actually a link to a soname file –libm.so libm.so.5 v At compile time, the soname filename is stored in the object file –no library code, so the object file is small link continued
38
240-491 Adv. UNIX: large/1338 v When the object file is executed, the soname is used to load the real shared library –the soname file is often just a link to the real library –libm.so.5libm.so.5.0.9 link
39
240-491 Adv. UNIX: large/1339 6.7. Some Common Shared Libraries libc.so standard C library libg++.so standard C++ library libgdbm.so GNU dbm database libncurses.so terminal-based graphics libtiff.so TIFF image manipulation
40
240-491 Adv. UNIX: large/1340 6.8. Dynamic Libraries v A dynamic library is a variant of a shared library –the library code is loaded at run time, but under the control of the program itself –the program can also unload a library –useful for programs that occasionally use very big libraries
41
240-491 Adv. UNIX: large/1341 6.9. The old '.sa' Format Older versions of Linux used the a.out binary file format –shared library filenames in that file format end with '.sa' –you should not see them in new Linux's
42
240-491 Adv. UNIX: large/1342 7. A Static Library Example v Static libraries are fine for libraries which will not be heavily used. v Creating shared libraries still varies between UNIXes –but static library creation has been standardized
43
240-491 Adv. UNIX: large/1343 7.1. A Static Library for Graphics graphics.h : typedef struct { int x, y, z;... } Image; void display_im(Image im); void move_im(Image im, int x, int y, int z); :
44
240-491 Adv. UNIX: large/1344 Function Implementations v idisplay.c: #include “graphics.h” void display_im(Image im) {... } v imove.c: #include “graphics.h” void move_im(Image im, int x, int y, int z) {... }
45
240-491 Adv. UNIX: large/1345 7.2. Creating the Static Library v Compile: $ gcc -c idisplay.c $ gcc -c imove.c Build a static library called glib.a : $ ar ruvs glib.a idisplay.o imove.o v The library is made from two object files and a symbol definition file (a list of function names, constants used in the “.o” files).
46
240-491 Adv. UNIX: large/1346 ar Options OperatorMeaning r Replace files. u Update files if changed. v Verbose. s Create symbol definition file.
47
240-491 Adv. UNIX: large/1347 Other Commands Some versions of ar do not have an “s” option, so you must use ranlib : $ ar ruv glib.a idisplay.o imove.o $ ranlib glib.a Listing the object files in a library with “t” : $ ar tv glib.a /* verbose listing */
48
240-491 Adv. UNIX: large/1348 7.3. Using the Library #include #include “graphics.h” : int main() { Image i;... display_im(i); foop(i); move_im(i, 2,3,4):... } main.c #include #include “graphics.h” : void foop(Image j) { Image i;... display_im(i); display_im(j);... } foop.c
49
240-491 Adv. UNIX: large/1349 Compilation / Linking v Compile and link: $ gcc -c main.c $ gcc -c foop.c $ gcc -o foo main.o foop.o glib.a The “.a” file must be at the end so that all the symbols uses in the object files are known. The linker links in the files from the “.a” library which contain functions, constants, etc. used by the “.c” code.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.