CSE250 – Data Structures in C++ Spring 2015 Jaroslaw ‘Jaric’ Zola

advertisement
CSE250 – Data Structures in C++
Spring 2015
Jaroslaw ‘Jaric’ Zola
http://www.jzola.org/
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
1/13
Code organiza on
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
2/13
Almost No Rules
• Unlike Java, C++ has no strictly enforced code
hierarchy/organiza on
• The primary constraint is the “one defini on rule”
• All other rules are pure conven on
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
3/13
Why Organizing?
Some good reasons (no par cular order):
• Split responsibility between developers
• Simplify code sharing and reuse
• Improve manageability
(e.g. commits)
• Speedup compila on
• Increase readability
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
4/13
How C++ Code is Built?
Source Files: *.hpp, *.cpp
Preprocessor (cpp): g++ -E file.cpp
Expanded Source Code
Compiler (g++): g++ -S file.cpp
Assembly Code: *.s
Assembler (as): g++ -c file.cpp
Binary Code: *.o
Linker (ld): g++ file.cpp
Final Executable: a.out
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
5/13
Header Files
• Specify (library) interface
• Contain declara ons or templates specifica ons
• O en include inline defini ons
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
6/13
Header Files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// File: point.hpp
#ifndef POINT_HPP
#define POINT_HPP
#include <iostream>
namespace cse250 {
class Point {
public:
// contructors
Point() : type_(0), x_(0), y_(0), z_(0) { }
// methods
// ...
private:
int type_;
float x_;
float y_;
float z_;
}; // class Point
std::ostream& operator<<(std::ostream&, const Point&);
} // namespace cse250
#endif // POINT_HPP
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
7/13
Header Files
Header files must contain “guards” to avoid viola on of one
defini on rule due to mul ple inclusion:
1
2
3
4
5
// File: H1.hpp
1
2
3
4
5
// File: H2.hpp
1
2
3
4
5
6
// File: F.cpp
struct Foo {
// ...
};
#include "H1.hpp"
// ...
#include "H1.hpp"
#include "H2.hpp" // Foo defined twice!
// ...
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
8/13
Source Files
• Specify (library) implementa on
• Contain actual defini ons
• Directly related to their headers
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
9/13
Source Files
1
#include "point.hpp"
2
3
namespace cse250 {
4
5
6
7
8
9
10
11
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << p.get_type() << " ("
<< p.get_x() << ","
<< p.get_y() << ","
<< p.get_z() << ")";
return os;
} // operator<<
12
13
} // namespace cse250
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
10/13
Manual Compila on
In the very last stage linker must have access to all defini ons!
1
2
3
4
5
6
7
8
9
10
// File: main.cpp
#include <iostream>
#include "point.hpp"
int main() {
cse250::Point p;
std::cout << p << std::endl;
return 0;
}
1
2
3
4
5
6
g++ main.cpp −o
1
g++ main.cpp point.cpp −o
/tmp/ccUX5ZiQ.o: In function `main':
main.cpp:(.text+0x21): undefined reference to `cse250::operator<<(std::ostream&,
cse250::Point const&)'
collect2: error: ld returned 1 exit status
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
11/13
Automa c Compila on
Large projects are never built manually!
• Use make and autotools
• Or even be er cmake
• Other solu ons exist (e.g. jam)
• Many IDEs automate automa on
Jaroslaw ‘Jaric’ Zola
CSE250 – Data Structures in C++
12/13
Download