Difference between revisions of "Uno/Cpp/Tutorials/Global References/Samples"
From Apache OpenOffice Wiki
< Uno | Cpp | Tutorials | Global References
m (Some tests for trying out the C++ statics behavior) |
m (Added another test ...) |
||
Line 87: | Line 87: | ||
</code> | </code> | ||
− | |||
dlopen_cBar.cxx | dlopen_cBar.cxx | ||
+ | <code>[cpp] | ||
#include "Foo.hxx" | #include "Foo.hxx" | ||
Line 114: | Line 114: | ||
} | } | ||
</code> | </code> | ||
− | |||
dlopen_cxxBar_uexit.cxx | dlopen_cxxBar_uexit.cxx | ||
Line 147: | Line 146: | ||
openBar("libBar.cxx.so"); | openBar("libBar.cxx.so"); | ||
− | dlclose(dlhandle); | + | dlclose(dlhandle); // Please note how important this is, not calling it renders |
+ | // this test invalid! | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | dlopen_cxxFoo_wo_closing.cxx | ||
+ | <code>[cpp] | ||
+ | #include "Foo.hxx" | ||
+ | |||
+ | #include <dlfcn.h> | ||
+ | #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; | return 0; | ||
Line 171: | Line 193: | ||
} | } | ||
</code> | </code> | ||
− | |||
plain.cxx | plain.cxx | ||
Line 185: | Line 206: | ||
</code> | </code> | ||
− | |||
− | |||
<pre> | <pre> | ||
.SUFFIXES: | .SUFFIXES: | ||
Line 202: | Line 221: | ||
libFoo.cxx.so \ | libFoo.cxx.so \ | ||
dlopen_cxxBar_uexit.cxx.bin \ | dlopen_cxxBar_uexit.cxx.bin \ | ||
− | localFoo.cxx.bin | + | localFoo.cxx.bin \ |
+ | dlopen_cxxFoo_wo_closing.cxx.bin | ||
run: build | run: build | ||
Line 211: | Line 231: | ||
./dlopen_cxxBar_uexit.cxx.bin | ./dlopen_cxxBar_uexit.cxx.bin | ||
./localFoo.cxx.bin | ./localFoo.cxx.bin | ||
+ | ./dlopen_cxxFoo_wo_closing.cxx.bin | ||
.PHONY: All build run | .PHONY: All build run |
Revision as of 10:02, 27 August 2007
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]
- 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]
- include "Foo.hxx"
- include <iostream>
- 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]
- include "Foo.hxx"
static Foo foo;
int main() {
foo.openBar("libBar.c.so");
return 0;
}
dlopen_cxxBar.cxx
[cpp]
- include "Foo.hxx"
static Foo foo;
int main() {
foo.openBar("libBar.cxx.so");
return 0;
}
dlopen_cxxBar_uexit.cxx
[cpp]
- include "Foo.hxx"
- include <unistd.h>
static Foo foo;
int main() {
foo.openBar("libBar.cxx.so");
_exit(0);
}
dlopen_cxxFoo.cxx
[cpp]
- include "Foo.hxx"
- include <dlfcn.h>
- 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]
- include "Foo.hxx"
- include <dlfcn.h>
- 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]
- include "Foo.hxx"
- include <dlfcn.h>
- 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]
- 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