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

Posted on 2004-08-25
Last Modified: 2010-04-21

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?

Question by:Risky101
LVL 45

Accepted Solution

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

Expert Comment

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...)
LVL 45

Expert Comment

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


Expert Comment

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
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.
LVL 45

Expert Comment

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

Expert Comment

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)

Author Comment

ID: 12222928
Thanks for this neat tip!!!

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 ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (, 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