Solved

Linking statically to the pthread library - without adding 500k to your app

Posted on 2004-08-25
7
480 Views
Last Modified: 2010-04-21
Hi,

Normally my app is 20k, but when I link it statically to the pthread library it balloons to 520k. The only thing we have used from the thread library is "create_thread". We are using an embedded system with limited resources (4Mb total) - is there any way to only add the object code that it needs?

Shane.
0
Comment
Question by:Risky101
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 11899804
If you are linking statically, there is no way to avoid including the library as part of your executable.

If the library is being shared by other apps on your system, you can link to the library dynamically (compile it to a .so)

Otherwise, if you need to save memory, look into the implementation of the library and trim it to be compilable with only the minimal set of functions that you need ... Substantial amount of work but worth the effort when memory is precious ...

Also, I think there are some light weight threading libraries, see if you can find one of them

good luck
0
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11957195
Odd, I thought that linking statically only linked the code that is actually used.  However, it may very well be that the create_thread call uses a lot of internal stuff in the library.  I assume when you're linking statically, you're linking to a .a file ?  In any case, you could try copying the pthread library code to your application source and compiling them there, linking to the .o files directly.  (But as far as I know, that shouldn't be different to linking to a .a file...)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11960918
>I thought that linking statically only linked the code that is actually used.  

Not exactly ... Entire archive member is bundled with the executable
http://www.linuxjournal.com/article.php?sid=6463
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11962105
Ah, I see.  An archive consists of multiple objects, and the linker links an entire object if it contains some unresolved symbol.  

quoted from http://www.linuxjournal.com/article.php?sid=6463
---
If input is an archive, it scans through the list of member modules that constitute the archive to match any unresolved symbols present in U. If some archive member defines any unresolved symbol that archive member is added to the list O, and U and D are updated per symbols found in the archive member. This process is iterated for all member object files.
---

Which means that the pthread library is essentially a single object, which gets linked in its entirety ?

In that case I'd still say grab the source code and add it to your source tree.  You need to check the license to see if you're allowed to do that of course.  It might be so that those 500kb are actually all needed code, i.e. that the threads library just has to do a lot of work.

Additionally, if you're trying to link statically to the pthreads library, won't it also link statically to a lot of other libs, like the C library ?  That could add a lot of size as well.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11962135
>Which means that the pthread library is essentially a single object, which gets linked in its entirety ?
Most likely yes ... Even otherwise, its a big chunk that is getting linked

>if you're trying to link statically to the pthreads library, won't it also link statically to a lot of other
>libs, like the C library ?
I do not think so ... Those libs should still be linked dynamically
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 12215203
I once worked on a project where I had to make my own mini linux distro to fit on a 8MB PC104 flash disk, with a program linked statically to the oracle client library so that it could interact with an oracle DB. The oracle client library alone was over 5MB, which presented a problem. I managed to use the "ar" tool to extract all the .o files from the library, and using "ar" again I made a new .a file which contained only the bare minimum object files I needed (obtained through trial and error and intelligent guesswork), which reduced the library to 500kB. Perhaps you can do something similar with the pthread library, assuming the create_thread call doesn't use much of the functionality contained in the pthreads library (e.g. mutexes, condition variables etc can probably all be left out if you're just creating a thread and not using anything else from the pthread library)
0
 

Author Comment

by:Risky101
ID: 12222928
Thanks for this neat tip!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now