LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-19-2010, 04:08 AM   #1
sathyadurai
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
Lightbulb Which is the best option to go with shared library?


I need a design which requires complete modularity and speed. I have a huge monolithic process that i'm seperating now with individual modules as libraries.

I'm just worried how to go about the no of shared libraries? for example can i have 10 shared libraries in place of 1? what will be the advantage in that case?
 
Old 11-19-2010, 09:18 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Dividing a program into libraries has the advantage of reducing the complexity of maintenance -- if you need to change a single or a few functions, you can do so in a library quickly and easily without needing to recompile an entire program (or any other libraries). Dividing functions into multiple libraries of related functions is a reasonable approach. The advantage here is maintenance and generally is useful if a lot of changes and updates are expected. This is, I believe, true of both static (libname.a) and shared libraries (libname.so).

It's probably always better to write from the perspective of main program plus one or more libraries. Other than the maintenance advantage, libraries are most useful when more than one program or more than one instance of a particular program is going to be executing at the same time (as in multiple users executing the same program simultaneously or multiple users executing different programs that use some of the same functions); that's where libraries, particularly shared libraries, shine. This is because shared libraries are loaded by programs when they start and any additional programs that start later will use the shared library without reloading it -- it's a little more complicated, but that's essentially what happens.

So, can you have more than one shared library? Sure. Can you have many? Sure. How many is a balancing act that takes into account maintenance (how often do you make changes or add functions), how much active work do you do on your application. On a modern system there really isn't that much overhead involved in loading multiple libraries; if the thing is stumbling over its own feet now just dividing it up into libraries isn't going to make a heckuva lot of difference. Just try to separate your functions into groups that make sense to you and you'll probably be all right.

Hope this helps some.
 
1 members found this post helpful.
Old 11-19-2010, 10:15 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sathyadurai View Post
a design which requires complete modularity and speed.
Why does it require modularity and/or what do you mean by "modularity".

Any big software project ought to be modular in its structure of source files. Modularity in the build sequence and dependencies is also likely a good idea. But none of that requires shared libraries.

Shared libraries might support modularity in the distribution process, but that generally doesn't work very well nor save enough to be worth the trouble.

The modularity of shared libraries is much more important when multiple executables need the same functions.

So what purpose / kind of modularity do you actually require?

There is some tradeoff vs. speed. Code in a .so can be noticeably slower than the same code in the main executable. Usually that is not a significant factor, but since you mentioned speed, you should be aware that there might be an issue.

Quote:
I'm just worried how to go about the no of shared libraries? for example can i have 10 shared libraries in place of 1? what will be the advantage in that case?
The run time disadvantages of ten .so's in place of one are generally smaller than the already small disadvantages of .so's in place of linking directly into the main executable. I don't think you want to worry about those disadvantages.

But why ask us what the advantages are? That totally depends on what you are doing. There is no generic advantage to .so at all and certainly no generic advantage to more .so files vs. fewer. The specific advantages depend on your situation:

Are you distributing updates through some limited or low speed channel and also updating different parts of the project on different schedules?

Are you creating several different executables that each need a different subset of a library of shared functions?

Those are the kind of issues that might push someone toward using a larger number of smaller .so files.

Quote:
Originally Posted by tronayne View Post
libraries are most useful when more than one program or more than one instance of a particular program is going to be executing at the same time
I think the advantage for shared libraries for more than one instance of a program is an obsolete concept. The main program image should be just as shareable between multiple instances as the shared libraries. The loader maps most of the main image into the virtual address space rather than reading it into anonymous memory. Like any other mapping of a file into memory, resident pages will be shared by any other process that maps the same file and has the same pages resident.

Shared libraries are most useful when more than one program (not instances of one program) uses the same functions.

Last edited by johnsfine; 11-19-2010 at 10:24 AM.
 
1 members found this post helpful.
  


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
shared library : libfg.so: cannot open shared object file error 40 x_gaurav Programming 2 06-28-2009 06:29 AM
ldd reports shared library missing, but library exists on disk athv_gr Linux - Newbie 7 05-13-2009 12:31 PM
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 11:15 AM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:13 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