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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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