Home > Archive > Unix Programming > November 2005 > Shared object linking problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Shared object linking problem
|
|
| izhar.wallach@gmail.com 2005-11-20, 6:59 pm |
| Hi,
I'm trying to write an application which have a component that I like
to modify without restarting it.
Thus, I was thinking of using a shared object for that component.
However, in the example below, I haven't figured out a way to modify
commonClass.cpp (for example, the print() method) and be able to see
the changes without rerunning the application. I can see changes made
directly in sharedClass.cpp, though. Is there a way to see changes in
commonClass.cpp without restarting the application?
Thanks,
Izhar.
(I have posted it at comp.lang.c++ before).
// main.cpp
#include <iostream>
#include <dlfcn.h>
#include <stdio.h>
#include <unistd.h>
#include "commonClass.h"
#include "sharedClass.h"
sharedClass* sharedClass_SO = NULL;
int main() {
while( true ) {
commonClass cc;
cc.print();
void *handle = dlopen ( "sharedClass.so", RTLD_NOW );
if( handle == NULL ) {
std::cerr << dlerror() << std::endl;
exit( -1 );
}
sharedClass_SO->print();
delete sharedClass_SO;
dlclose( handle );
int x;
std::cin >> x;
}
return 0;
}
// commonClass.h
#ifndef _COMMON_CLASS_H_
#define _COMMON_CLASS_H_
class commonClass {
public:
commonClass(){};
void print();
};
#endif
//commonClass.cpp
#include <iostream>
#include "commonClass.h"
void commonClass::print() {
// I CAN'T see changes here without re-running the application
std::cout << "commonClass: XXXXXX" << std::endl;
}
// sharedClass.h
#ifndef _SHARED_CLASS_H_
#define _SHARED_CLASS_H_
class sharedClass {
public:
sharedClass(){};
virtual ~sharedClass() {};
virtual void print();
};
extern sharedClass* sharedClass_SO;
#endif
// sharedClass.cpp
#include <iostream>
#include "sharedClass.h"
#include "commonClass.h"
void sharedClass::print() {
commonClass cc;
// I CAN see changes here without re-running the application
std::cout << "sharedClass: ";
cc.print();
}
extern "C" {
sharedClass* maker() {
return new sharedClass();
}
class proxy {
public:
proxy() {
sharedClass_SO = maker();
}
};
proxy p;
}
// Makefile
all: main sharedClass.so
commonClass.o: commonClass.cpp
g++ -Wall -c commonClass.cpp
sharedClass.o: sharedClass.cpp commonClass.o
g++ -Wall -c sharedClass.cpp
sharedClass.so: sharedClass.o
g++ sharedClass.o -shared -o sharedClass.so
main.o: main.cpp
g++ -rdynamic -Wall -c main.cpp
main: main.o commonClass.o
g++ -rdynamic main.o commonClass.o -ldl -o main
clean:
/bin/rm -f main *.o *.so
| |
| Ulrich Eckhardt 2005-11-21, 3:58 am |
| [nothing to do with GCC, dropping that from the list of NGs]
izhar.wallach@gmail.com wrote:
> Thus, I was thinking of using a shared object for that component.
> However, in the example below, I haven't figured out a way to modify
> commonClass.cpp (for example, the print() method) and be able to see
> the changes without rerunning the application. I can see changes made
> directly in sharedClass.cpp, though.
Let's see:
> commonClass.o: commonClass.cpp
> g++ -Wall -c commonClass.cpp
> sharedClass.o: sharedClass.cpp commonClass.o
> g++ -Wall -c sharedClass.cpp
> sharedClass.so: sharedClass.o
> g++ sharedClass.o -shared -o sharedClass.so
> main.o: main.cpp
> g++ -rdynamic -Wall -c main.cpp
>
> main: main.o commonClass.o
> g++ -rdynamic main.o commonClass.o -ldl -o main
commonClass is linked directly into the main executable, so there is no way
to change it at runtime. sharedClass is loaded dynamically, so it could be
replaced by another at runtime.
> Is there a way to see changes in commonClass.cpp without restarting
> the application?
No, unless you structure it like sharedClass, that is.
Uli
--
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/
|
|
|
|
|