Escolar Documentos
Profissional Documentos
Cultura Documentos
Today
Separate compilation
Name collisions
Assignments
None outstanding
Page 1
void main(){
x = 3;
bar();
}
Page 2
Function Visibility
Static vs. dynamic scope: Important because
they resolve global variables
Function visibility: Something of a separate
issue:
What functions can you see from where?
Page 3
P3
P4
F1
Page 4
Separate Compilation
What happens when you have classes and functions
spread across multiple compilation units?
In file foo.c:
void foo(){.};
In file main.c:
void main(){foo();}
Page 5
Page 6
foo.c:
#include foo.h
int foo::a(){
}
int foo::b(){
}
Page 7
Main Function
#include foo.h
void main(){
foo f;
f.a();
f.b();
}
Page 8
main.cpp:
#include <iostream.h>
class foo{
};
#include foo.h
#include <iostream.h>
void main()
.
}
Page 9
Page 10
foo.c:
void main(){
extern int x;
x = 3;
}
int x;
Page 11
Compilation:
Compiling foo.java to foo.class is straightforward (no other code files involved).
Compiling main.java to main.class requires that the compiler look for the code
for foo (e.g., to check function calls in terms of type and number of parameters)
Compiler uses the classpath variable (must be set as part of the run-time
environment configuration) to look for foo in this case
Source code for foo is not needed (only the .class file is needed)
Page 12
File inclusion
More primitive idea
Have to worry about redundant includes
Language really doesnt address the problem directly.
Must separate interface from implementation or you really dont have
separate compilation
Java:
No direct copying as with file inclusion
Dont have to separate interface from implementation to achieve
separate compilation
Less efficient (involves some searching to find server classes)
Less primitive than what happens in C++
Page 13
Name Conflicts
class String
void main(){
String s;
..
}
class String
Page 14
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
Page 15
Main Program
Set the classpath to refer to the location of foo
class main {
public static main(){
foo.ugh u = new foo.ugh;
u.a()
}
}
Page 16
Importing a Package
You can import a package and eliminate the need for
qualified naming, e.g.:
import foo.*; (or import foo.ugh)
class main {
public static main(){
ugh u = new ugh;
u.a()
}
}
Page 17
Namespaces in C++
Analogous to the package concept in Java:
namespace foo{
class blah{
};
class 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 2003 CS 403 Class Notes
Page 18
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++.
Page 19