Uno/Cpp/Tutorials/Global References/Samples

From Apache OpenOffice Wiki
< Uno‎ | Cpp‎ | Tutorials‎ | Global References
Revision as of 10:02, 27 August 2007 by Kr (Talk | contribs)

Jump to: navigation, search

Foo.hxx [cpp] struct Foo {

   void * m_dlhandle;
   Foo();
   ~Foo();
   void openBar(char const * libName);

};


extern "C" void openBar(char const * libName);

Bar.cxx [cpp]

  1. include <iostream>


struct Bar {

   int m_a;
   Bar();
   ~Bar();

};

Bar::Bar() : m_a(1) {

   std::cerr << __PRETTY_FUNCTION__ << std::endl;

}

Bar::~Bar() {

   m_a = 0;
   std::cerr << __PRETTY_FUNCTION__ << std::endl;

}


static Bar bar;

extern "C" int Bar_checkValid() {

   return bar.m_a;

}

Foo.cxx [cpp]

  1. include "Foo.hxx"
  1. include <iostream>
  1. include <dlfcn.h>


Foo::Foo() : m_dlhandle(NULL) {

   std::cerr << __PRETTY_FUNCTION__ << std::endl;

}

Foo::~Foo() {

   std::cerr << __PRETTY_FUNCTION__ << std::endl;
   int (*Bar_checkValid )() = (int (*)())dlsym(m_dlhandle, "Bar_checkValid");
   
   if (Bar_checkValid())
       std::cerr << "\tBar is valid -> statics are OK!" << std::endl;
   
   else
       std::cerr << "\tBar is invalid -> statics are BAD!" << std::endl;
       
   if (m_dlhandle) {
       std::cerr << "\tclosing Bar..." << std::endl;
       dlclose(m_dlhandle);
   }

}

void Foo::openBar(char const * libName) {

   m_dlhandle = dlopen(libName, RTLD_NOW);
   std::cerr << __PRETTY_FUNCTION__ << "- handle:" << m_dlhandle << std::endl;

}


extern "C" void openBar(char const * libName) {

   static Foo foo;
   foo.openBar(libName);

}

dlopen_cBar.cxx [cpp]

  1. include "Foo.hxx"


static Foo foo;

int main() {

   foo.openBar("libBar.c.so");
   return 0;

}

dlopen_cxxBar.cxx [cpp]

  1. include "Foo.hxx"


static Foo foo;

int main() {

   foo.openBar("libBar.cxx.so");
   return 0;

}

dlopen_cxxBar_uexit.cxx [cpp]

  1. include "Foo.hxx"
  1. include <unistd.h>


static Foo foo;

int main() {

   foo.openBar("libBar.cxx.so");
   _exit(0);

}

dlopen_cxxFoo.cxx [cpp]

  1. include "Foo.hxx"
  1. include <dlfcn.h>
  2. include <iostream>


int main() {

   void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
   std::cerr << __PRETTY_FUNCTION__ << "- handle(libFoo.cxx.so):" << dlhandle << std::endl;
   void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
   openBar("libBar.cxx.so");
   dlclose(dlhandle); // Please note how important this is, not calling it renders
   // this test invalid!
   return 0;

}

dlopen_cxxFoo_wo_closing.cxx [cpp]

  1. include "Foo.hxx"
  1. include <dlfcn.h>
  2. include <iostream>


int main() {

   void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
   std::cerr << __PRETTY_FUNCTION__ << "- handle(libFoo.cxx.so):" << dlhandle << std::endl;
   void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
   openBar("libBar.cxx.so");
   //dlclose(dlhandle); // Please note how important this is, not calling it renders
   // this test invalid!
   return 0;

}

localFoo.cxx [cpp]

  1. include "Foo.hxx"
  1. include <dlfcn.h>
  2. include <iostream>


int main() {

   void * dlhandle = dlopen("libBar.cxx.so", RTLD_NOW|RTLD_GLOBAL);
   std::cerr << __PRETTY_FUNCTION__ << "- handle(libBar.cxx.so):" << dlhandle << std::endl;
   static Foo foo;
   foo.m_dlhandle = dlhandle;
   return 0;

}

plain.cxx [cpp]

  1. include "Foo.hxx"


static Foo foo;

int main() {

   return 0;

}

.SUFFIXES:


All: run


build: \
 plain.cxx.bin \
 dlopen_cBar.cxx.bin libBar.c.so \
 dlopen_cxxBar.cxx.bin  \
 dlopen_cxxFoo.cxx.bin \
 libBar.cxx.so \
 libFoo.cxx.so \
 dlopen_cxxBar_uexit.cxx.bin \
 localFoo.cxx.bin \
 dlopen_cxxFoo_wo_closing.cxx.bin

run: build
	./plain.cxx.bin
	./dlopen_cBar.cxx.bin
	./dlopen_cxxBar.cxx.bin
	./dlopen_cxxFoo.cxx.bin
	./dlopen_cxxBar_uexit.cxx.bin
	./localFoo.cxx.bin
	./dlopen_cxxFoo_wo_closing.cxx.bin

.PHONY: All build run


%.cxx.o: %.cxx
	g++ -g -c -fpic $^ -o $@

%.c.o: %.c
	gcc -g -c -fpic $^ -o $@

lib%.c.so: %.c.o
	gcc -shared $^ -o $@

lib%.cxx.so: %.cxx.o
	g++ -shared $^ -o $@

%.cxx.bin: %.cxx.o
	g++ $^ -o $@ -ldl


plain.cxx.bin: plain.cxx.o Foo.cxx.o libBar.cxx.so
dlopen_cBar.cxx.bin: dlopen_cBar.cxx.o Foo.cxx.o
dlopen_cxxBar.cxx.bin: dlopen_cxxBar.cxx.o Foo.cxx.o
dlopen_cxxFoo.cxx.bin: dlopen_cxxFoo.cxx.o
localFoo.cxx.bin: localFoo.cxx.o Foo.cxx.o
dlopen_cxxBar_uexit.cxx.bin: dlopen_cxxBar_uexit.cxx.o Foo.cxx.o



clean:
	rm -f *.o *.so *.bin
Personal tools