Difference between revisions of "Uno/Cpp/Tutorials/Global References/Samples"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Added another test ...)
m (now builds on Solaris as well ...)
Line 27: Line 27:
  
 
Bar::Bar() : m_a(1) {
 
Bar::Bar() : m_a(1) {
     std::cerr << __PRETTY_FUNCTION__ << std::endl;
+
     std::cerr << "Bar::Bar()" << std::endl;
 
}
 
}
  
Line 33: Line 33:
 
     m_a = 0;
 
     m_a = 0;
  
     std::cerr << __PRETTY_FUNCTION__ << std::endl;
+
     std::cerr << "Bar::~Bar()" << std::endl;
 
}
 
}
  
Line 53: Line 53:
  
  
Foo::Foo() : m_dlhandle(NULL) {
+
Foo::Foo() : m_dlhandle(RTLD_DEFAULT) {
     std::cerr << __PRETTY_FUNCTION__ << std::endl;
+
     std::cerr << "Foo::Foo()" << std::endl;
 
}
 
}
  
 
Foo::~Foo() {
 
Foo::~Foo() {
     std::cerr << __PRETTY_FUNCTION__ << std::endl;
+
     std::cerr << "Foo::~Foo()" << std::endl;
  
 
     int (*Bar_checkValid )() = (int (*)())dlsym(m_dlhandle, "Bar_checkValid");
 
     int (*Bar_checkValid )() = (int (*)())dlsym(m_dlhandle, "Bar_checkValid");
Line 76: Line 76:
 
void Foo::openBar(char const * libName) {
 
void Foo::openBar(char const * libName) {
 
     m_dlhandle = dlopen(libName, RTLD_NOW);
 
     m_dlhandle = dlopen(libName, RTLD_NOW);
     std::cerr << __PRETTY_FUNCTION__ << "- handle:" << m_dlhandle << std::endl;
+
     std::cerr << "Foo::openBar(char const * libname)" << "- handle:" << m_dlhandle << std::endl;
 
}
 
}
  
Line 141: Line 141:
 
int main() {
 
int main() {
 
     void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
 
     void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
     std::cerr << __PRETTY_FUNCTION__ << "- handle(libFoo.cxx.so):" << dlhandle << std::endl;
+
     std::cerr << "main" << "- handle(libFoo.cxx.so):" << dlhandle << std::endl;
  
 
     void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
 
     void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
Line 163: Line 163:
 
int main() {
 
int main() {
 
     void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
 
     void * dlhandle = dlopen("libFoo.cxx.so", RTLD_NOW);
     std::cerr << __PRETTY_FUNCTION__ << "- handle(libFoo.cxx.so):" << dlhandle << std::endl;
+
     fprintf(stderr, "main - handle(libFoo.cxx.so): %p\n", dlhandle);
  
 
     void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
 
     void (* openBar)(char const *) = (void (*)(char const * libNanme))dlsym(dlhandle, "openBar");
Line 185: Line 185:
 
int main() {
 
int main() {
 
     void * dlhandle = dlopen("libBar.cxx.so", RTLD_NOW|RTLD_GLOBAL);
 
     void * dlhandle = dlopen("libBar.cxx.so", RTLD_NOW|RTLD_GLOBAL);
     std::cerr << __PRETTY_FUNCTION__ << "- handle(libBar.cxx.so):" << dlhandle << std::endl;
+
     std::cerr << "main" << "- handle(libBar.cxx.so):" << dlhandle << std::endl;
  
 
     static Foo foo;
 
     static Foo foo;
Line 208: Line 208:
 
<pre>
 
<pre>
 
.SUFFIXES:
 
.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)
  
  
Line 233: Line 259:
 
./dlopen_cxxFoo_wo_closing.cxx.bin
 
./dlopen_cxxFoo_wo_closing.cxx.bin
  
.PHONY: All build run
+
clean:
 +
rm -f *.o *.so *.bin
  
 +
.PHONY: All build run clean
  
 
%.cxx.o: %.cxx
 
%.cxx.o: %.cxx
g++ -g -c -fpic $^ -o $@
+
$(CXX) -g -c $(CFLAGS) $^ -o $@
  
 
%.c.o: %.c
 
%.c.o: %.c
gcc -g -c -fpic $^ -o $@
+
$(CC) -g -c $(CFLAGS) $^ -o $@
  
 
lib%.c.so: %.c.o
 
lib%.c.so: %.c.o
gcc -shared $^ -o $@
+
$(LINK_CC_LIB) $^ -o $@
  
 
lib%.cxx.so: %.cxx.o
 
lib%.cxx.so: %.cxx.o
g++ -shared $^ -o $@
+
$(LINK_CXX_LIB) $^ -o $@
  
 
%.cxx.bin: %.cxx.o
 
%.cxx.bin: %.cxx.o
g++ $^ -o $@ -ldl
+
$(LINK_CXX) $^ -o $@ -ldl
  
  
Line 258: Line 286:
 
localFoo.cxx.bin: localFoo.cxx.o Foo.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_cxxBar_uexit.cxx.bin: dlopen_cxxBar_uexit.cxx.o Foo.cxx.o
 
+
dlopen_cxxFoo_wo_closing.cxx.bin: dlopen_cxxFoo_wo_closing.cxx.o
 
+
 
+
clean:
+
rm -f *.o *.so *.bin
+
 
</pre>
 
</pre>

Revision as of 08:40, 28 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]

  1. 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 [cpp]

  1. include "Foo.hxx"
  1. include <iostream>
  1. 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 [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 << "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 [cpp]

  1. include "Foo.hxx"
  1. include <dlfcn.h>
  2. 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 [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 << "main" << "- 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:


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
Personal tools