- add test9 which implements a class entirely in a module

This commit is contained in:
Bryce Denney 2002-10-16 14:55:10 +00:00
parent 855e2007a0
commit 0c3efe4236
8 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,53 @@
top_builddir = ..
top_srcdir = @srcdir@/..
srcdir = @srcdir@
VPATH = @srcdir@
CXX=@CXX@
CXXFLAGS=@CXXFLAGS@ @INCLTDL@
LDFLAGS=@LDFLAGS@ @LT_LDFLAGS@
LIBS=@LIBS@
LTDL_STATIC_LIB=${top_builddir}/libltdl/.libs/libltdlc.al
LIBTOOL=@LIBTOOL@
RPATH=`pwd`/lib
# select whether to use libtool or win32-specific target.
# This should either be all_libtool or all_win32_dlltool.
all: @PLUGIN_MAKEFILE_TARGET@
########### libtool makefile for all platforms except win32 ###########
all_libtool: uselib libmodule1.la libmodule2.la
uselib: main.lo
$(LIBTOOL) $(CXX) -export-dynamic $(LDFLAGS) -o uselib main.lo $(LIBS) @LIBLTDL@
lib%.la: %.lo
$(LIBTOOL) $(CXX) -module $(LDFLAGS) -o $@ $< -rpath ${RPATH}
mkdir -p lib bin
$(LIBTOOL) cp $@ ${RPATH}
%.lo: %.cc
$(LIBTOOL) $(CXX) $(CXXFLAGS) -c $<
#######################################################################
############# makefile for building plugin DLLs for win32 #############
all_win32_dlltool: uselib.exe module1.dll module2.dll
uselib.exe: main.cc main.h
$(CXX) $(CXXFLAGS) -c -DDLL_EXPORT -o main.o ${srcdir}/main.cc
dlltool --output-def uselib.def main.o
dlltool --dllname uselib.exe --def uselib.def --output-lib uselib.a
dlltool --dllname uselib.exe --output-exp uselib.exp --def uselib.def
$(CXX) $(CXXFLAGS) -o uselib.exe uselib.exp main.o ${LIBS} ${LTDL_STATIC_LIB}
#rm uselib.exp uselib.def
%.dll: %.o uselib.exe
$(CXX) $(CXXFLAGS) -shared -o $@ $< uselib.a
%.o: %.cc
$(CXX) $(CXXFLAGS) -c $<
#######################################################################
include ${top_srcdir}/common-make-defs.txt
clean: clean-common

View File

@ -0,0 +1,6 @@
test9-modclass
Try to put a class definition in a module, and call it from the
executable. The definition of ModuleGadget is ONLY defined in
module1, and no class is defined in module2. I was just trying
to keep it simple, and take one step at a time.

View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define LT_SCOPE extern /* so that ltdl.h does not export anything */
#include <ltdl.h>
#define MAIN_DLL_EXPORT
#include "main.h"
#include "modules.h"
const char *version_string = "uselib-test6-1.0";
int register_module (const char *name)
{
printf ("register_module was called by module '%s'\n", name);
return 0;
}
int load_module (const char *fmt, const char *modname)
{
char buf[512];
sprintf (buf, fmt, modname);
printf ("loading module from VARIES{%s}\n", buf);
lt_dlhandle handle = lt_dlopenext (buf);
printf ("handle is VARIES{%p}\n", handle);
if (!handle) {
printf ("lt_dlopen error: %s\n", lt_dlerror ());
return -1;
}
modload_func func = (modload_func) lt_dlsym (handle, "module_init");
printf ("module_init function is at VARIES{%p}\n", func);
if (func != NULL) {
printf ("Calling module_init\n");
ModuleGadget *gadget = (*func)();
if (gadget) {
printf ("%s's gadget is called '%s'\n", modname, gadget->getName ());
printf ("%s can do '%s'\n", modname, gadget->getFeatures ());
} else {
printf ("%s has not defined any gadget\n", modname);
}
} else {
printf ("lt_dlsym error: %s\n", lt_dlerror ());
return -1;
}
return 0;
}
int main (int argc, char **argv)
{
printf ("start\n");
if (lt_dlinit () != 0) {
printf ("lt_dlinit error: %s\n", lt_dlerror ());
return -1;
}
#ifdef WIN32
const char *module_name_format = "%s";
#else
const char *module_name_format = "lib%s.la";
#endif
printf ("loading module1\n");
// try to load module1
if (load_module (module_name_format, "module1") < 0) {
printf ("load module1 failed\n");
}
if (load_module (module_name_format, "module2") < 0) {
printf ("load module2 failed\n");
}
int arg;
for (int arg=1; arg < argc; arg++) {
if (load_module (module_name_format, argv[arg]) < 0) {
printf ("load %s failed\n", argv[arg]);
}
}
printf ("stop\n");
exit (77);
}

View File

@ -0,0 +1,18 @@
#if defined(WIN32) || defined(__CYGWIN__)
# ifdef MAIN_DLL_EXPORT
# ifdef DLL_EXPORT
# warning I will export DLL symbols for MODULE1
# define MAINAPI(type) __declspec(dllexport) type
# endif
# else
# warning I will import DLL symbols for MODULE1
# define MAINAPI(type) __declspec(dllimport) type
# endif
#endif
#ifndef MAINAPI
# warning No DLL import/export is needed
# define MAINAPI(type) type
#endif
MAINAPI(extern const char *) version_string;
MAINAPI(extern int) register_module (const char *name);

View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include "main.h"
#include "modules.h"
class ModuleGadget* module_init ()
{
printf ("module1 init for main version %s\n", version_string);
register_module ("module1");
return new ModuleGadget ("FancyCellPhone", "Caller ID, Video Conferencing");
}
int operate (int a, int b)
{
return a + b;
}
//////// ModuleGadget class methods
ModuleGadget::ModuleGadget(const char* name, const char* features)
{
this->name = name;
this->features = features;
}
const char* ModuleGadget::getName ()
{
return name;
}
const char* ModuleGadget::getFeatures ()
{
return features;
}

View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include "main.h"
#include "modules.h"
int n_operations = 0;
class ModuleGadget* module_init ()
{
printf ("module2 init for main version %s\n", version_string);
register_module ("module2");
return NULL;
}
int operate (int a, int b)
{
return a - b;
}

View File

@ -0,0 +1,16 @@
extern "C" {
// this prevents C++ name mangling
class ModuleGadget * module_init ();
};
typedef class ModuleGadget* (*modload_func)(void);
class ModuleGadget {
int id;
const char *name;
const char *features;
public:
ModuleGadget(const char* name, const char* features);
virtual const char* getName ();
virtual const char* getFeatures ();
};

View File

@ -0,0 +1,18 @@
start
loading module1
loading module from VARIES{xxx}
handle is VARIES{xxx}
module_init function is at VARIES{xxx}
Calling module_init
module1 init for main version uselib-test6-1.0
register_module was called by module 'module1'
module1's gadget is called 'FancyCellPhone'
module1 can do 'Caller ID, Video Conferencing'
loading module from VARIES{xxx}
handle is VARIES{xxx}
module_init function is at VARIES{xxx}
Calling module_init
module2 init for main version uselib-test6-1.0
register_module was called by module 'module2'
module2 has not defined any gadget
stop