Difference between revisions of "Uno/Cpp/Tutorials/Global References/Samples"
From Apache OpenOffice Wiki
< Uno | Cpp | Tutorials | Global References
m (now builds on Solaris as well ...) |
m (Categorizing) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
Foo.hxx | Foo.hxx | ||
− | < | + | <source lang="cpp"> |
struct Foo { | struct Foo { | ||
void * m_dlhandle; | void * m_dlhandle; | ||
Line 12: | Line 12: | ||
extern "C" void openBar(char const * libName); | extern "C" void openBar(char const * libName); | ||
− | </ | + | </source> |
Bar.cxx | Bar.cxx | ||
− | < | + | <source lang="cpp"> |
#include <iostream> | #include <iostream> | ||
Line 42: | Line 42: | ||
return bar.m_a; | return bar.m_a; | ||
} | } | ||
− | </ | + | </source> |
Foo.cxx | Foo.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 85: | Line 85: | ||
foo.openBar(libName); | foo.openBar(libName); | ||
} | } | ||
− | </ | + | </source> |
dlopen_cBar.cxx | dlopen_cBar.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 99: | Line 99: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
dlopen_cxxBar.cxx | dlopen_cxxBar.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 113: | Line 113: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
dlopen_cxxBar_uexit.cxx | dlopen_cxxBar_uexit.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 129: | Line 129: | ||
_exit(0); | _exit(0); | ||
} | } | ||
− | </ | + | </source> |
dlopen_cxxFoo.cxx | dlopen_cxxFoo.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 151: | Line 151: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
dlopen_cxxFoo_wo_closing.cxx | dlopen_cxxFoo_wo_closing.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 173: | Line 173: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
localFoo.cxx | localFoo.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 192: | Line 192: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
plain.cxx | plain.cxx | ||
− | < | + | <source lang="cpp"> |
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 204: | Line 204: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
<pre> | <pre> | ||
Line 288: | Line 288: | ||
dlopen_cxxFoo_wo_closing.cxx.bin: dlopen_cxxFoo_wo_closing.cxx.o | dlopen_cxxFoo_wo_closing.cxx.bin: dlopen_cxxFoo_wo_closing.cxx.o | ||
</pre> | </pre> | ||
+ | |||
+ | [[Category:UNO-API]] |
Latest revision as of 23:07, 17 March 2010
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