DOC PREVIEW
UW CSE 303 - Linking and Libraries

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsWinter 2009Lecture 27— Linking and LibrariesCSE 303 Winter 2009, Lecture 27 1'&$%Intro to linkingLinking is just one example of “using stuff in other files”...In compiling and running code, one constantly needs other files andprograms that find them.Examples:• C preprocessor #include• C libraries (where is the code for printf and malloc)• Java source files (referring to other source code)• Java class files (referring to other compiled code)Usually you’re happy with programs “automatically finding what youneed” so the complicated rules can be hidden.Today we w ill demys tify and make generalizations.CSE 303 Winter 2009, Lecture 27 2'&$%Common questions1. What you are looking for?2. When are you looking for it?3. Where are you looking?4. What problems do cyc les cause?5. How do you change the answers?our old friends: files, function names, paths, environment variables,command-line flags, scripts, configuration files, ...CSE 303 Winter 2009, Lecture 27 3'&$%Include files - what really happens?cpp (invoked implicitly by gcc or g++ on files ending in .c, .cc, etc.).What: files named “foo” when encountering #include <foo> or#include "foo" (note .h is just a convention).When: When the preprocessor is run (making x.i from x.c).Where: “include path” current-directory, directories chosen when cppis installed (e.g., /usr/include), directories listed in INCLUDE shellvariable, directories listed via -I flags, ...The rules on “what overrides what” exist, but tough to remember.Can look at result to see “what really happened”.Example: for nes ted #include, the original current-directory or theheader file’s current-directory?Example: Why shouldn’t you run cpp on 1 machine and compile theresults on another?CSE 303 Winter 2009, Lecture 27 4'&$%javac is similarIf A.java defines class A to have a field of type B, how “does thecompiler know w hat B is”?What: a file named B.class (probably the result of compilingB.java).When: When compiling a source file that uses the class B.Where: “class path” current-directory, directories chosen when javacwas installed, directories listed in CLASSPATH shell variables, directorieslisted via -classpath flags, ... (Note: Packages correspond tosubdirectories)The rules on “what overrides what” exist, but tough to remember.CSE 303 Winter 2009, Lecture 27 5'&$%Source code cyclesWhat if two source files refer to each other?• C: Can’t but don’t need to: Put declarations in header files andinclude each header file at most once.• Java: If B.class is not found, but B.java is, (implicitly) compileB.java (potentially with information the compiler already hasabout A).– javac implicitly discovers and acts on dependencies.CSE 303 Winter 2009, Lecture 27 6'&$%IDEsFancier developme nt environments provide help with “packages”,“projects”, etc .Fundamentally, the questions are the same and their are settings andmenu items for controlling your development proc ess .CSE 303 Winter 2009, Lecture 27 7'&$%Compiled codeSo far we have talked about finding source code to create compiledcode (either .o files for C or .class files for Java).These files are not whole applications, so we have the sam e quest ionsfor “finding the other code”.The Java story is a bit sim pler, so we will do it first.CSE 303 Winter 2009, Lecture 27 8'&$%Java class-loading and executionRecall java A args runs class A’s s tatic main me thod with args.java is just a program that finds A.class and knows what to do(interpretation and/or just-in-time compilation).But it will probably have to find lots of other classfiles too.Simple (untrue but doable) version: Recursively find all the class filesyou need before starting execution:• What: class files referred to• When: start of execution• Where: classpath, etc.Disadvantages?CSE 303 Winter 2009, Lecture 27 9'&$%Java class-loading continuedActually, the JVM is much lazier (technical word) about class-loading;waiting until a class is actually used (tec hnical definition) duringexecution.That is, the when is “later” and “more com plicated”.So is the where:• jar files (lots of classes in one file, retrieved together)• remote class files (applets with code over the web, etc.)• different security settings for class es found different placesWhy use a jar (“Java arc hive”) file:• Keep classes that need each other together• Faster/simpler re mote retrievalCSE 303 Winter 2009, Lecture 27 10'&$%Object code is differentA .o file is not “runnable” – you have to actually link it with theother code to make an executable.Linking (ld, or called via gcc or g++) is a “when” between compilingand executing.Again, gcc hides this from you (just -c or not -c), but it helps toknow what is going on.First discuss static linking, which is mostly like the untrue version ofJava we sketched.CSE 303 Winter 2009, Lecture 27 11'&$%LinkingIf a C file uses but does not define a function (or global variable) foo,then the .o has “unresolved references”. Declarations don’t count;only definitions.The linker takes multiple .o files and “patches them” to include thereferences. (It literally moves code and changes instructions likefunction calls.)An executable must have no unresolved references (you have seen thiserror mess age).What: Definitions of functions/variablesWhen: The linker c reates an executableWhere: Other .o files on the command-line (and much more...)CSE 303 Winter 2009, Lecture 27 12'&$%More about whereThe linker and O/S don’t know anything about main or the C library.That’s why gcc “secretly” links in other things.We c an do it ourselves , but we would need to know a lot about howthe C library is organized. Get gcc to tell us:• gcc -v -static hello.c• Should be largely understandable.• -static (stick with the simple “get all the code you need intoa.out story)• the secret *.o files: (they do the stuff before main gets called,which is why gcc gives errors about main not being defined).• -lc: complicated story about finding the library (a.k.a. “archive”)libc.a and including any files that provide still-unresolvedreferences.CSE 303 Winter 2009, Lecture 27 13'&$%ArchivesAn archive is the “.o equivalent of a .jar file” (though history is theother way around).Create with ar program (lots of features, but fundamentally take .ofiles and put them in, but order matte rs).The semantics of passing


View Full Document

UW CSE 303 - Linking and Libraries

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Linking and Libraries
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Linking and Libraries and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Linking and Libraries 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?