By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

Shared object linking problem

P: n/a
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?


// 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;
void *handle = dlopen ( "", RTLD_NOW );
if( handle == NULL ) {
std::cerr << dlerror() << std::endl;
exit( -1 );
delete sharedClass_SO;
dlclose( handle );
int x;
std::cin >> x;
return 0;

// commonClass.h
#ifndef _COMMON_CLASS_H_
#define _COMMON_CLASS_H_
class commonClass {
void print();

#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 {
virtual ~sharedClass() {};
virtual void print();

extern sharedClass* sharedClass_SO;

// 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: ";

extern "C" {
sharedClass* maker() {
return new sharedClass();
class proxy {
proxy() {
sharedClass_SO = maker();
proxy p;

// Makefile
all: main

commonClass.o: commonClass.cpp
g++ -Wall -c commonClass.cpp
sharedClass.o: sharedClass.cpp commonClass.o
g++ -Wall -c sharedClass.cpp sharedClass.o
g++ sharedClass.o -shared -o
main.o: main.cpp
g++ -rdynamic -Wall -c main.cpp

main: main.o commonClass.o
g++ -rdynamic main.o commonClass.o -ldl -o main

/bin/rm -f main *.o *.so

Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
iz*********** wrote:

You're OT here, I'm afraid.

You might try:


Good luck.
Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.