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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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 Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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