Question: Q 2 . ( a ) What is the difference between a statically linked library ( or archive ) and a dynamically linked library (

Q2.(a) What is the difference between a statically linked library (or archive) and
a dynamically linked library (or shared object)? State the advantages and
disadvantages of each. For each type of library, give an example where it would
be the preferred choice. [8]
Scrutinise the code for a small library given in Figure Q2. Throughout this question,
line numbers are included only for ease of reference and do not constitute part of
the program.
(b) What would happen if a source file accidentally included the header file twice?
Suggest and enhancement to overcome this problem. [5]
The following small program is written which uses this library:
1 #include
2 #include "apse.h"
3 int main()
4{
5 One one;
6 Counter c(one.get());
7 std::cout << c.get()<<''<< c.get()<< std::endl;
8 return 0;
9}
Object files are produced from the source files, and a shared library is created using
the command
g++-fPIC -shared one.o counter.o -o libapse.so
The main program (called try) is then linked using the command
g++-o try main.o -L.-lapse
(c) Why is the compiler option -fPIC necessary when building the shared library?
[4]
(d) What is the function of the -L. option in building the program? [4]
(e) Running the program with ./try produces the message
./try: error while loading shared libraries: libapse.so: cannot
open shared object file: No such file or directory.
However, the file libapse.so exists in the build directory. What is the remedy
for this? [4]
Page 4 of 13/OVER
1// File: apse.h
2 struct One {
3 int get(void);
4};
5
6 class Counter {
7 public:
8 Counter(int start=0);
9 int get(void);
10 private:
11 int i;
12};
1// File: apse1.cxx
2 #include "apse.h"
3 int One::get(void){ return 1; };
1// File apse2.cxx
2 #include "apse.h"
3 Counter::Counter(int start) : i {start}{}
4 int Counter::get(void){ return i++; }
Figure Q2: Source code for the apse library

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!