shared object problem
Oct 16, 2010 at 6:17am UTC
Hello all,
I'm writing a program that uses (one at a time) many similar objects based on a common one with some data/functions overwritten. I thought that would be easier to dynamically load the custom objects using polymorphism.
This is the setup:
baseclass - common data and functions
childclass1...n - different types of operations on the objects
module1..n - object with specific data and/or functions overwritten
mainprogram - loads modules/objects based on program arguments and executes some function in them
The problem is that running the main program gives me a segfault :(
Here are the files (might be more clear than my explanations :) )
$ ls -1 *.cpp *.h
baseclass.cpp
baseclass.h
childclass.cpp
childclass.h
mainprogram.cpp
module1.cpp
module2.cpp
file: baseclass.cpp
1 2 3 4 5 6 7
#include "baseclass.h"
#include <iostream>
using namespace std;
void BaseClass::execute(){ cout<<"executed from BaseClass x is " << x << endl; }
file: baseclass.h
1 2 3 4 5 6 7 8 9 10 11
#ifndef BASECLASS_FILE_HEADER_INC
#define BASECLASS_FILE_HEADER_INC
class BaseClass{
public :
int x;
virtual void execute();
};
#endif /* ----- #ifndef BASECLASS_FILE_HEADER_INC ----- */
file: childclass.cpp
1 2 3 4 5 6 7 8
#include "childclass.h"
#include <iostream>
using namespace std;
void ChildClass::execute(){
cout << "executed from ChildClass x is " << x << endl;
}
file: childclass.h
1 2 3 4 5 6 7 8 9 10 11 12
#ifndef CHILDCLASS_FILE_HEADER_INC
#define CHILDCLASS_FILE_HEADER_INC
#include "baseclass.h"
class ChildClass: public BaseClass{
public :
void execute();
};
#endif /* ----- #ifndef CHILDCLASS_FILE_HEADER_INC ----- */
file: mainprogram.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include "baseclass.h"
//#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
void *handle;
BaseClass *a=NULL;
char *error, dll[9];
if (argc==1) strcpy(dll,"./module1.so" );
else strcpy(dll,"./module2.so" );
handle = dlopen (dll, RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
a = (BaseClass*)dlsym(handle, "obj" );
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
a->execute();
dlclose(handle);
return 0;
}
file: module1.cpp
1 2 3 4 5 6 7 8 9 10
#include "childclass.h"
#include "baseclass.h"
class Module1: public ChildClass{
public :
Module1(){
int x=1;
}
} mobj;
BaseClass *obj=&mobj;
file: module2.cpp
1 2 3 4 5 6 7 8 9 10
#include "childclass.h"
#include "baseclass.h"
class Module1: public ChildClass{
public :
Module1(){
int x=2;
}
} mobj;
BaseClass *obj=&mobj;
compilation:
g++ -c baseclass.cpp -o baseclass.o
g++ -c childclass.cpp -o childclass.o
g++ -c module1.cpp -o module1.o
g++ -shared module1.o baseclass.o childclass.o -o module1.so
g++ -c module2.cpp -o module2.o
g++ -shared module2.o baseclass.o childclass.o -o module2.so
g++ -c mainprogram.cpp -o mainprogram.o
g++ mainprogram.o baseclass.o -o mainprogram -ldl
./mainprogram
Segmentation fault (core dumped)
Any ideas?
Thx.
Oct 16, 2010 at 10:19am UTC
Topic archived. No new replies allowed.