CS631 -- Advanced Programming in the UNIX Environment - Exercise

Implement the 'libgreet' library.

Summary

In this assignment you will write a simplistic shared library. The functionality it provides is defined in the greet(3) manual page. Sample usage might look like so:


$ cat hello.c
#include <stdio.h>

#include "greet.h"

int main(void) {
	greet();
	if (setgreeting("Howdy!") != 0) {
		fprintf(stderr, "Unable to set greeting!\n");
	}
	greet();
	hello("world", getgreeting());
	return 0;
}
$ cc -Wall hello.c -lgreet
$ ./a.out
Hello!
Howdy!
world: Howdy!
$ 

Create a directory for your library, say "libgreet". In it, begin by creating a header file greet.h, which includes the forward declarations of the functions provided in the library. With that in place, you can already compile (although not link) your main program, e.g.: cc -Wall -c hello.c -I/home/${USER}/libgreet.

Next, create the file greet.c, which implements the functionality of the library. With that in place, you should be able to compile your program all in one go (i.e. not using your code as a shared library):


$ cc -Wall -c libgreet/greet.c -o libgreet/greet.o
$ cc -Wall -c hello.c -I./libgreet
$ cc hello.o libgreet/greet.o
$ ./a.out
Hello!
Howdy!
world: Howdy!
$ 

Finally, use what you learned in class in lecture 10 to build a shared library libgreet.so so that you then can compile your program like this:


$ cc -Wall hello.c -I./libgreet -L./libgreet -lgreet


[Course Website]