Static lib on Linux

I have a program that is statically linked for Linux so users don’t need to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire thing
gets linked into the program.

I don’t need sound or networking, just basic graphics. How can I build a
custom libSDL.a that only contains the functionality I need and can be linked
from the project directory so it doesn’t replace the standard libSDL.a?

Also, since I’ve been unable to get anything to work on Mac OSX, I’d
appreciate it if someone were to volunteer to compile the program for Macs.
The program computes and plots linear regression for statistics students.

Thanks,
Jeff

I have a program that is statically linked for Linux so users don’t need to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire thing
gets linked into the program.

You can also dynamically link for Linux and ship libSDL.so. For this to
work nicely, you link using the $ORIGIN feature of the linker. Here’s
some info:

http://blogs.sun.com/dipol/entry/dynamic_libraries_rpath_and_mac

I don’t need sound or networking, just basic graphics. How can I build a
custom libSDL.a that only contains the functionality I need

./configure --help

You can also build SDL and your program (and everything else you link
statically) with the following flags, both for gcc as well as g++
(CFLAGS/CXXFLAGS):

-fdata-sections -ffunction-sections

And for the link step (LDFLAGS):

-Wl,–gc-sections

This will result in the compiler removing unused code in the final binary.On 05/06/2011 02:56 PM, Jeff Post wrote:

I have a program that is statically linked for Linux so users don’t need to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a
is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire thing
gets linked into the program.

First, if you are trying to minimize size, then on Linux you should compile
SDL (and your program, assuming C/C++) with a "minimize code size"
optimization flag, and without debug information. On GCC, those are “-Os”
(minimize size) and “-g0” (disable debug).

I don’t need sound or networking, just basic graphics. How can I build a
custom libSDL.a that only contains the functionality I need and can be
linked
from the project directory so it doesn’t replace the standard libSDL.a?

It looks like the compile-time defines:

SDL_VIDEO_DISABLED
SDL_AUDIO_DISABLED
SDL_TIMERS_DISABLED
SDL_JOYSTICK_DISABLED
SDL_JOYSTICK_DISABLED

control what modules are built. I’d use Nikos’ suggestions of doing
"./configure --help" to get the “proper” way to set these. If you don’t mind
do a bit of manual effort, you can rename the resulting static link library
to something special like “libSDLcustom.a” and store it in some directory
relative to your project root. For example, if you had your directory like:

myfile.c
SDLstuff/libSDLcustom.a

You might do:

gcc myfile.c -LSDLstuff/ -lSDLcustom

The -L part adds the directory “SDLstuff” to the linker search path for
libraries. The last -l tells the linker to look for libSDLcustom.a, which of
course, will only be found inside of the “SDLstuff” folder.

Also, since I’ve been unable to get anything to work on Mac OSX, I’dOn Fri, May 6, 2011 at 6:56 AM, Jeff Post <j_post at pacbell.net> wrote:

appreciate it if someone were to volunteer to compile the program for Macs.
The program computes and plots linear regression for statistics students.

Thanks,
Jeff


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

when you statically link a library, only the sections of code that are
used (aka the functions that are called from the executable) are added
to the final executable (plus some code overhead)
more here: http://stackoverflow.com/questions/5332609/c-and-c-static-linking-just-a-copy

VittorioOn Fri, May 6, 2011 at 1:56 PM, Jeff Post <j_post at pacbell.net> wrote:

I have a program that is statically linked for Linux so users don’t need to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire thing
gets linked into the program.

Not on Linux; the whole lib is included there. Same in OS X. In my
other posts I explained how to make GCC and ld do that.On 05/06/2011 05:11 PM, Vittorio G. wrote:

On Fri, May 6, 2011 at 1:56 PM, Jeff Post<j_post at pacbell.net> wrote:

I have a program that is statically linked for Linux so users don’t need to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire thing
gets linked into the program.

when you statically link a library, only the sections of code that are
used (aka the functions that are called from the executable) are added
to the final executable (plus some code overhead)
more here: http://stackoverflow.com/questions/5332609/c-and-c-static-linking-just-a-copy

I have a program that is statically linked for Linux so users don’t need
to
have SDL installed on their machines. For Windows, I dynamically link and
provide SDL.dll which is only about a quarter megabyte. However, libSDL.a
is
a whopping 2.28 megabytes (this is SDL version 1.2.9) and the entire
thing
gets linked into the program.

when you statically link a library, only the sections of code that are
used (aka the functions that are called from the executable) are added
to the final executable (plus some code overhead)
more here:
http://stackoverflow.com/questions/5332609/c-and-c-static-linking-just-a-copy

Not on Linux; the whole lib is included there. Same in OS X. In my other
posts I explained how to make GCC and ld do that.

The behavior Nikos is seeing is correct. Just because you may only call
SDL_Init(SDL_INIT_VIDEO), doesn’t mean the code for handling, say
SDL_INIT_EVERYTHING, goes away. He could still possibly call it, so the
other subsystems’ code must be included – and thus the larger libraries.
Only a compiler that can actually perform intermodule analysis and
optimization could mark that code as dead and remove it, and that’s some
pretty advanced analysis.

You have the actually remove it from the call graph. Open SDL.c and check
out the code for SDL_Init() to see what I mean. There are a bunch of #if
#endif blocks around the different subsystems. If those evaluated to
false, then I suspect the code will dramatically drop in size. I hope this
helps.

Also, doing “strip -s” on the resulting binary should minimize the size.

PatrickOn Fri, May 6, 2011 at 10:22 AM, Nikos Chantziaras wrote:

On 05/06/2011 05:11 PM, Vittorio G. wrote:

On Fri, May 6, 2011 at 1:56 PM, Jeff Post<j_post at pacbell.net> wrote:

Apparently that only works if I recompile SDL with those flags. Adding them to
my application makefile still results in a 2.88 Mb binary, whereas the
dynamically linked version is 47Kb.

When time permits, I’ll try rebuilding SDL. Thanks to all for the replies.

JeffOn Friday 06 May 2011 05:16, Nikos Chantziaras wrote:

You can also build SDL and your program (and everything else you link
statically) with the following flags, both for gcc as well as g++
(CFLAGS/CXXFLAGS):

-fdata-sections -ffunction-sections

And for the link step (LDFLAGS):

-Wl,–gc-sections

This will result in the compiler removing unused code in the final binary.