LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2008, 03:00 PM   #1
lodziarz
LQ Newbie
 
Registered: Sep 2007
Posts: 7

Rep: Reputation: 0
Dynamic Libraries in Linux - C++


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

Last edited by lodziarz; 03-22-2008 at 05:11 PM.
 
Old 03-22-2008, 10:08 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
I haven’t didn’t look at anything except the Makefile, but I think it would be easier for you to do:
Code:
LD = g++
and btw, CC is traditionally the abbreviation for the C Compiler.
 
Old 03-22-2008, 11:01 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I would just do this:
Code:
MODULES_DIR = modules/SpamCatch
...
libSpamCatch.so: SpamCatch.o
	$(LD) -shared -o $(MODULES_DIR)/libSpamCatch.so $(MODULES_DIR)/SpamCatch.o

$(APP): tools.cpp
	$(CC) -o $(APP) tools.cpp -L$(MODULES_DIR) -lSpamCatch -lpthread
It's worth noting:

1. You don't need to explicitly call "dlopen" or "dlsym" (the compiler, linker and runtime will do the right things for you)

2. Linux shared libraries do *not* contain any state.

In other words, the fact that you're doing threading is more or less irrelevant to the fact that you're also using shared libraries. This sometimes comes as a surprise to people coming from the Windows world...

Here's a good article on the subject:
http://tldp.org/HOWTO/Program-Librar...libraries.html

'Hope that helps .. pSM

Last edited by paulsm4; 03-22-2008 at 11:06 PM.
 
Old 03-23-2008, 05:30 AM   #4
lodziarz
LQ Newbie
 
Registered: Sep 2007
Posts: 7

Original Poster
Rep: Reputation: 0
yep, but then adding new module needs to recompile whole program... that's what I wanted to avoid... Main loop needs to be running without any break and it has to be connected to the server... and in meanwhile I'll code new modules, compile them and load them dynamically by commands comming from server main loop is connected to... f.e. I code & compile module x.so and I send command from server LOAD x.so... and it loads it and uses as rest... this same with unloading.

Thanks for all your help.

Greetings,
Michal

//EDIT: Problem with __dso_handle solved: used http://www.linuxquestions.org/questi...ream-c-331113/
But still question stays... am I solving that right way?

Last edited by lodziarz; 03-23-2008 at 09:37 AM.
 
Old 03-23-2008, 02:44 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Nope - adding a "brand new module" involves compiling *something* (your new code), but only involves relinking (vis. your actual program).
 
Old 03-24-2008, 02:42 PM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
check out: http://aconole.brad-x.com/programs/nosak-demo.tar.bz2

I have a quick 'n dirty solution already written (albeit in C). Has a dispatcher thread, and a "sniffing" thread. The application loads a plugin from the plugins/ directory. In this case, it loads the ethernet plugin.

Just a demo of how to do something that you've described here that I started writing a while ago but never completed.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
static and dynamic libraries nesta Programming 11 09-04-2011 08:36 PM
Dynamic link libraries eshwar_ind Programming 1 05-09-2004 11:02 PM
Linux Version of Dynamic Link Libraries paul_eniki Programming 4 02-05-2004 09:25 AM
Mixing Shared and Dynamic Libraries linuxeco Programming 1 02-02-2003 09:04 PM
linux dynamic libraries aizkorri Programming 4 10-09-2002 03:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration