Uno/Cpp/Tutorials/Global References/Samples
From Apache OpenOffice Wiki
< Uno | Cpp | Tutorials | Global References
Revision as of 16:09, 15 April 2008 by SergeMoutou (Talk | contribs)
Foo.hxx
struct Foo { void * m_dlhandle; Foo(); ~Foo(); void openBar(char const * libName); }; extern "C" void openBar(char const * libName);
Bar.cxx
#include <iostream> struct Bar { int m_a; Bar(); ~Bar(); }; Bar::Bar() : m_a(1) { std::cerr << "Bar::Bar()" << std::endl; } Bar::~Bar() { m_a = 0; std::cerr << "Bar::~Bar()" << std::endl; } static Bar bar; extern "C" int Bar_checkValid() { return bar.m_a; }
Foo.cxx
#include "Foo.hxx" #include <iostream> #include <dlfcn.h> Foo::Foo() : m_dlhandle(RTLD_DEFAULT) { std::cerr << "Foo::Foo()" << std::endl; } Foo::~Foo() { std::cerr << "Foo::~Foo()" << 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 << "Foo::openBar(char const * libname)" << "- handle:" << m_dlhandle << std::endl; } extern "C" void openBar(char const * libName) { static Foo foo; foo.openBar(libName); }
dlopen_cBar.cxx
#include "Foo.hxx" static Foo foo; int main() { foo.openBar("libBar.c.so"); return 0; }
dlopen_cxxBar.cxx
#include "Foo.hxx" static Foo foo; int main() { foo.openBar("libBar.cxx.so"); return 0; }
dlopen_cxxBar_uexit.cxx
#include "Foo.hxx" #include <unistd.h> static Foo foo; int main() { foo.openBar("libBar.cxx.so"); _exit(0); }
dlopen_cxxFoo.cxx
#include "Foo.hxx" #include <dlfcn.h> #include <iostream> int main() { void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW); std::cerr << "main" << "- 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
#include "Foo.hxx" #include <dlfcn.h> #include <iostream> int main() { void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW); fprintf(stderr, "main - handle(libFoo.cxx.so): %p\n", dlhandle); 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
#include "Foo.hxx" #include <dlfcn.h> #include <iostream> int main() { void * dlhandle = dlopen("libBar.cxx.so", RTLD_NOW|RTLD_GLOBAL); std::cerr << "main" << "- handle(libBar.cxx.so):" << dlhandle << std::endl; static Foo foo; foo.m_dlhandle = dlhandle; return 0; }
plain.cxx
#include "Foo.hxx" static Foo foo; int main() { return 0; }
.SUFFIXES: ifeq ($(VENDOR),sun) CC := cc CXX := CC SHARED := -G CPPRT := -lCstd CFLAGS := -KPIC RPATH := -R. else CC := gcc CXX := g++ SHARED=-shared CPPRT := CFLAGS := -fpic RPATH := -Wl,-rpath,. endif LINK_CC := $(CC) $(RPATH) LINK_CC_LIB := $(LINK_CC) $(SHARED) $(RPATH) LINK_CXX := $(CXX) $(CPPRT) $(RPATH) LINK_CXX_LIB := $(LINK_CXX) $(SHARED) $(RPATH) 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 clean: rm -f *.o *.so *.bin .PHONY: All build run clean %.cxx.o: %.cxx $(CXX) -g -c $(CFLAGS) $^ -o $@ %.c.o: %.c $(CC) -g -c $(CFLAGS) $^ -o $@ lib%.c.so: %.c.o $(LINK_CC_LIB) $^ -o $@ lib%.cxx.so: %.cxx.o $(LINK_CXX_LIB) $^ -o $@ %.cxx.bin: %.cxx.o $(LINK_CXX) $^ -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 dlopen_cxxFoo_wo_closing.cxx.bin: dlopen_cxxFoo_wo_closing.cxx.o