Docsity

CS 403 Class Notes - Spring 2002: Separate Compilation, Packages, Namespaces, and Linking, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 14 – Tuesday, 2/26/02
• Today
– Separate compilation
– Packages and namespaces
– Static vs. dynamic linking
• Reading Assignments For Next Class
– Read Chapter 3.6.2
• Assignments
– Assignment #4 due two weeks from today (March 12th)
• Class schedule
– Thursday, February 28 (project day – no class)
– Tuesday, March 5 (project day – no class)
– Next meeting: Thursday, March 7
Spring 2002 CS 403 Class Notes Page 2
Separate Compilation
• Program code distributed over multiple files
• Already addressed certain issues:
– How an object declared in one file is made visible
in another file
– The transitive inclusion problem in C++
• Next problem: Global name conflicts
pf3
pf4
pf5

Partial preview of the text

Download CS 403 Class Notes - Spring 2002: Separate Compilation, Packages, Namespaces, and Linking and more Study notes Programming Languages in PDF only on Docsity!

Spring 2002 CS 403 Class Notes (^) Page 1

Lecture 14 – Tuesday, 2/26/

  • Today
    • Separate compilation
    • Packages and namespaces
    • Static vs. dynamic linking
  • Reading Assignments For Next Class
    • Read Chapter 3.6.
  • Assignments
    • Assignment #4 due two weeks from today (March 12 th)
  • Class schedule
    • Thursday, February 28 (project day – no class)
    • Tuesday, March 5 (project day – no class)
    • Next meeting: Thursday, March 7

Spring 2002 CS 403 Class Notes (^) Page 2

Separate Compilation

• Program code distributed over multiple files

• Already addressed certain issues:

  • How an object declared in one file is made visible

in another file

  • The transitive inclusion problem in C++

• Next problem: Global name conflicts

Spring 2002 CS 403 Class Notes (^) Page 3

Name Conflicts

void main(){ String s; ….. }

class String …

class String …

Frequently, some classes might show up in multiple header files.

Spring 2002 CS 403 Class Notes (^) Page 4

Java Solution

  • Define packages: package foo; public class blah{ public int a(){…} public int c(){…} }; public class ugh{ public int b(){…} public int d(){…} };
  • Packages may be split over multiple files
  • If multiple files:
    • Store all files related to the package in jar file or directory

Spring 2002 CS 403 Class Notes (^) Page 7

Namespaces in C++

  • Analogous to the package concept in Java: namespace foo{ class blah{ }; class ugh{ }; }
  • This requires that classes in foo be referenced by the namespace qualifier: - foo::blah instead of blah - foo::ugh instead of ugh
  • You can “import” a namespace and eliminate the need for the qualifier, but like in Java, this eliminates the benefit of the namespace: - using namespace foo;

Spring 2002 CS 403 Class Notes (^) Page 8

Assignment

• Two basic elements:

  • Dealing with the transitive include problem in C++

and showing how the problem does not arise in

Java.

  • Dealing with global name collisions in both C++

and Java

• Look at assignment specification

Spring 2002 CS 403 Class Notes (^) Page 9

Static vs. Dynamic Linking

  • Static linking:
    • Prior to run time, linker combines binary files for separate components together into a single executable.
    • This approach used in Visual C++.
  • Dynamic linking:
    • Individual binary files for the separate components are loaded on demand at run-time.
    • This approach used in Java (JDK).
    • This approach also used by Microsoft with Dynamic Link Libraries, which may be implemented in Visual C++.

Spring 2002 CS 403 Class Notes (^) Page 10

Building a DLL in Visual Studio

  • Start a new project. Choose “Win32 DLL”.
  • Create an empty project (no files).
  • Write the actual code for the project.
  • Include the stanza:
    • __declspec(dllexport)
    • Goes right after the word “class” for each class declaration that you wish to export.
  • Compile and build the DLL.

Spring 2002 CS 403 Class Notes (^) Page 13

Demonstrations

• Static and dynamic linking with C++ and

Visual Studio

• Dynamic linking with Java and JDK