In: Computer Science
4.2.1 Dynamic linking in practice Here you must write a program that in the simplest way demonstrates the principle by having the instruction sequence of two different functions' only accessible through two different, dynamically linked, libraries (which you have built yourself). The program has the following specification: -All prints are made to the command prompt window. -Each printout step is followed by a line feed and carriage return. -At program start "Program start" is written -The program then calls a function located in one of the two dynamically linked libraries. This function prints "I.dll number one." -The program then calls a function located in the other dynamically linked library. This function prints out "I.dll number two." - The program exits.
This exercise has nothing to do with linked lists, its only about dynamic linking of libraries.
The code should be in C programming language and every step should be well commented for good understanding!
I have provided separate code for each file as asked in the question. I have also added comments in all the code snippets for clear and better understanding of the answer. Please find the answer attached below.
Ans) This is the first dll file and is called the "first_dll.c"
/* This is the first dll file and is called the "first_dll.c" */
#include<stdio.h> // This is for standard input and ouput
#include "first_dll.h" // This preprocessor directive tells the compilet to include or access "first_dll.h" in "first_dll.c"
EXPORT void first_print(void){
/* This is the print statement which the question demands and
the EXPORT keyword is defined in "first_dll.h" file */
printf (" I.dll number one \r\n");
//The print statement has the carriage return and line feed as expected in the question
}
The code below is the header file for the program "first_dll.c" which has the definition for the header file "first_dll.h" EXPORT keyword
/* Consider the file "first_dll.h" */
#define EXPORT __declspec(dllexport)
/* The __declspec(dllexport) is a DLL export directive which helps the linker to ger the function first_pirnt() in the "first_dll.c" program */
EXPORT void first_print(void);
NOW lets write the second DLL files in the similar manner :
/* This is the second dll file and is called the "second_dll.c" */
#include<stdio.h> // This is for standard input and ouput
#include "second_dll.h" // This preprocessor directive tells the compilet to include or access "second_dll.h" in "second_dll.c"
EXPORT void second_print(void){
/* This is the print statement which the question demands and
the EXPORT keyword is defined in "second_dll.h" file */
printf (" I.dll number two \r\n");
//The print statement has the carriage return and line feed as expected in the question
}
The code below is the header file for the program "second_dll.c" which has the definition for the header file "second_dll.h" and EXPORT keyword :
/* Consider the following code for file "second_dll.h" */
#define EXPORT __declspec(dllexport)
/* The __declspec(dllexport) is a DLL export directive which helps the linker to ger the function second_print() in the "second_dll.c" program */
EXPORT void second_print(void);
Now consider the main program "dll_linking.c" which calls the methods from dll written above :
/*program "dll_linking.c" which calls the methods from dll */
#include "first_dll.h"
#include "second_dll.h"
int main() {
printf(" \n Program start \r\n "); // the question says this message should be printed
first_print(); /* calling the function from dll1 i.e first_dll.c
which in turn has header of first_dll.h.This wil print "I.dll number one" with carriage return and line feed */
second_print(); /* calling the function from dll2 i.e second_dll.c
which in turn has header of second_dll.h. This wil print "I.dll number two" with carriage return and line feed */
return 0; //exit the program
}
/* NOTE THAT : The dll files should be compiled using the commands -->
gcc -c first_dll.c
gcc -shared -o First_Dll.dll -Wl,--out-implib,libtstdll.a first_dll.o
and
gcc -c second_dll.c
gcc -shared -o Second_Dll.dll -Wl,--out-implib,libtstdll.a second_dll.o
and for main program use commands-->
gcc -o dll_linking main.c -L. -lFirst_Dll -lSecond_Dll
*/