Hi!
I'm beginner in dynamic libraries thing. I want to build app, which will have 2 parts: main part and modules. Main part has to connect to server and in loop reads all traffic from it. Then it passes all what it reads to modules, and modules react for that. Modules has to have ability to send something to server also. Another important thing - it has to be multi-threaded. Main code reads line from server and runs X threads (X - modules count)... 1 thread = 1 module. The question is: how to do that? I've just started work with dynamic libraries, I modified easiest example found here:
http://bogomip.net/blog/dynamic-libr...n-c-and-linux/ but I can't compile it... I don't know, what I'm doing wrong...
tools.cpp:
Code:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <dlfcn.h>
using namespace std;
int main() {
void *handle;
void (*ReadRaw)(string);
char *error;
handle = dlopen("modules/SpamCatch/SpamCatch.so", RTLD_LAZY);
if(handle == NULL) {
fprintf(stderr, "Error: open/load error of dynamic.so failed: %s\n", dlerror());
exit(1);
}
ReadRaw = (void(*)(string)) dlsym(handle, "ReadRaw");
if((error = dlerror()) != NULL) {
fprintf(stderr, "Error: symbol lookup in dynamic.so failed: %s\n", dlerror());
exit(2);
}
ReadRaw("Here will be incoming line from server");
dlclose(handle);
return 0;
}
SpamCatch.cpp:
Code:
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
extern "C" void _init();
extern "C" void ReadRaw(string);
extern "C" void _fini();
void _init() {
printf("Initialization Phase\n");
}
void ReadRaw(string bleh) {
cout << "Text: " << bleh << endl;
}
void _fini() {
printf("Deconstruction Phase\n");
}
Makefile:
Code:
# Makefile
APP = tools
CC = g++
LD = ld
all: $(APP) modules
SpamCatch.o: modules/SpamCatch/SpamCatch.cpp
$(CC) -fPIC -I. -c modules/SpamCatch/SpamCatch.cpp -o modules/SpamCatch/SpamCatch.o
SpamCatch.so: SpamCatch.o
$(LD) -shared -o modules/SpamCatch/SpamCatch.so modules/SpamCatch/SpamCatch.o
$(APP): tools.cpp
$(CC) -o $(APP) tools.cpp -ldl -lpthread
modules: SpamCatch.so
clean:
@rm -f modules/SpamCatch/*.o modules/SpamCatch/*.so $(APP)
Compiles ok... but when I run:
Code:
$ ./tools
Error: open/load error of dynamic.so failed: modules/SpamCatch/SpamCatch.so: undefined symbol: __dso_handle
Maybe you have another ideas to solve this problem? Or maybe you can advice some tricks/ideas how to build those soft in easiest way?
Thanks for all your help!
Greetings,
Michal