'Base class' shared libraries in C?

Max Okumoto okumoto at ucsd.edu
Tue Mar 29 13:11:42 PST 2005


Jonathon McKitrick wrote:
Hi all,

I am trying to implement some C++ functionality in C.

Suppose I have several libraries that share behavior and properties, such as
version, working directory, and so on.  I want other libraries to extend
this behavior without having to duplicate any code.  For instance:
typedef struct base
{
	char version[32];
	char path[32];
} base_t;
I would not use *_t as the name of the typedef.  It might
conflict with a standard C or POSIX defintion in the future.
Just use base, since that allows you to hide the definition.
It will reduce your compile times when you modify a header file.
/* base.h */
typedef struct base {
	int	num;
	/* stuff */
} base;
/* app.h */
struct base;		/*
			 * base.h does not need to be included
			 * in app.h since only references are
			 * passed.
			 */
struct AppStuff {
	struct base	*info;
};
void foo(struct base *);

/* app.c */
#include "base.h"	/*
			 * base.h is included because bar() uses
			 * the num field.
			 */
static void
bar(base *b)
{
	printf("%d\n", b->num);
}
void
foo(base *b)
{
	bar(b);
}
			Max Okumoto




typedef struct foo
{
	base_t *base;
	int data;
} foo_t;
typedef struct bar
{
	base_t *base;
	int data;
} bar_t;
lib-base.so:
	base_get_version(base_t *, char *);
	base_get_path(base_t *, char *);
lib-derived-foo.so:
	foo_one_method(foo_t *, int data);
	foo_two_method(foo_t *, int data);
lib-derived-bar.so:
	bar_one_method(bar_t *, int data);
	bar_two_method(bar_t *, int data);
I want the derived modules to link with the base library, so that my
application can link with only the derived modules and still be able to call
the base functions without having to link to that library or call 'wrapper'
functions in the derived modules.
Is there a way to do this?

jm





More information about the Users mailing list