Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-08-25
Medium Priority
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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

609 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