LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2012, 09:48 PM   #1
c0d1f1ed
Member
 
Registered: Mar 2004
Location: Ottawa, Ontario
Distribution: Ubuntu 12.04
Posts: 30

Rep: Reputation: 15
Question Optimizing shared library size


Hi all, I've ported code for a Windows DLL to Linux, but the shared library is over four times bigger than the DLL!

It's a fairly big code base but only a small number of functions is exported from the DLL (using a .def file). I found out that the static library on the other hand exports all functions. So what I believe is happening is that on Windows all functions which are unused by the exported functions get stripped from the DLL.

So my question is, how can I specify the functions that should be exported from the shared library, and how do I get the linker to minimize the binary size based on that limited set?
 
Old 08-01-2012, 10:30 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
strip(1) might help to reduce the size
 
Old 08-02-2012, 03:22 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Here are some macros I've hacked to control exorting symbols from a shared object:

Code:
#ifndef DLL_PUBLIC
  #if defined (_Windows)
    #if defined (__GNUC__)
      #define DLL_PUBLIC(type,name) __attribute__ ((dllexport)) type name
      #define DLL_LOCAL(type,name) type name
    #else
      #define DLL_PUBLIC(type,name) type _export name
      #define DLL_LOCAL(type,name) type name
    #endif

  #elif defined (__unix__) || defined (__UNIX__)
    #define DLL_PUBLIC(type,name) __attribute__ ((visibility ("default"))) type name
    #define DLL_LOCAL(type,name)  __attribute__ ((visibility ("hidden"))) type name
  #endif
#endif
usage example:

Code:
DLL_LOCAL (long, HttpValue) (const ConstBuffData *b, int base) {...}
DLL_PUBLIC (int, Http_Mime) (const ConstBuffData *b, HTTP_MIME *ret) {...}
Here HttpValue won't be exported from the shared object, but HttpMime will.

Last edited by NevemTeve; 08-02-2012 at 04:14 AM.
 
1 members found this post helpful.
Old 08-02-2012, 03:32 AM   #4
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
Some browsing landed me to the below links. Please check, if it helps you.

http://www.rt-embedded.com/blog/arch...zation-part-1/
http://www.rt-embedded.com/blog/arch...zation-part-2/
http://www.rt-embedded.com/blog/arch...zation-part-3/
 
Old 08-02-2012, 04:34 PM   #5
c0d1f1ed
Member
 
Registered: Mar 2004
Location: Ottawa, Ontario
Distribution: Ubuntu 12.04
Posts: 30

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by NevemTeve View Post
strip(1) might help to reduce the size
I'm already doing that. The shared library is still much, much bigger than the DLL.
 
Old 08-03-2012, 10:55 AM   #6
c0d1f1ed
Member
 
Registered: Mar 2004
Location: Ottawa, Ontario
Distribution: Ubuntu 12.04
Posts: 30

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by NevemTeve View Post
Here are some macros I've hacked to control exorting symbols from a shared object:

Code:
#ifndef DLL_PUBLIC
  #if defined (_Windows)
    #if defined (__GNUC__)
      #define DLL_PUBLIC(type,name) __attribute__ ((dllexport)) type name
      #define DLL_LOCAL(type,name) type name
    #else
      #define DLL_PUBLIC(type,name) type _export name
      #define DLL_LOCAL(type,name) type name
    #endif

  #elif defined (__unix__) || defined (__UNIX__)
    #define DLL_PUBLIC(type,name) __attribute__ ((visibility ("default"))) type name
    #define DLL_LOCAL(type,name)  __attribute__ ((visibility ("hidden"))) type name
  #endif
#endif
usage example:

Code:
DLL_LOCAL (long, HttpValue) (const ConstBuffData *b, int base) {...}
DLL_PUBLIC (int, Http_Mime) (const ConstBuffData *b, HTTP_MIME *ret) {...}
Here HttpValue won't be exported from the shared object, but HttpMime will.
Unfortunately that's not practical since there are many thousands of functions, and there's lots of third party source code. However, this did lead me to a GCC option to hide all functions by default: http://gcc.gnu.org/wiki/Visibility I have high hopes that this will reduce the library size to something more acceptable. So thanks for pointing me in this direction!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 11:15 AM
What does optimizing for size mean? Thaidog Linux - General 3 10-27-2007 03:03 AM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration