Re: [gtk-list] NEWBIE: Static Linking



On Fri, 27 Aug 1999 09:50:58 -0600, Matt Long wrote:
> I'm sure this is a stupid question, but I just don't know the answer.
> Yesterday Aaron, Faby posted a message on why his app was so big and how
> to compile it so that it was dynamically linked. The answer that he came
> up with sounded like it might be related to my question. I would like to
> be able to ensure the opposite. I want to ensure my application is
> statcially linked. I thought the -g had something to do with that, but
> apparently (according to the man page) -g is just for debugging info. I
> also noticed in the man page that there is a linker flag there called
> -static. So what I was wondering is, what is the default action when
> compiling gtk+ apps with gcc. Do they link statically automatically or
> do I have to specify some kind of flag (-static, or something)?

The "-g" flag is indeed used to generate debug information which is in no
way related to having a static or dynamic linked binary. Debug information
is for debuggers only. Remove it with the strip utility.

(I assume that you are using gcc and GNU ld on a Linux platform, because
"static linking" is *very* platform specific.)

The normal behaviour of the linker is to link a library dynamically if a
dynamic (.so) and a static version (.a) of that library are available. If
there is only a static version available, that one will be used. You can
modify the behaviour of the linker by specifying that you want to link all
libraries static, or switch between static and dynamic linking.

To create an all static binary:

  gcc -static -o foo foo.o bar.o `gtk-config --libs`

To create a binary with the GTK and X11 libraries linked statically, but
the C library dynamically:

  gcc -o foo foo.o bar.o -Wl,-Bstatic `gtk-config --libs` -Wl,-Bdynamic

The last "-Wl,-Bdynamic" seems a bit strange but this is to tell the
linker that you want the C library explicitly linked dynamically.

This example only links the GTK+ libraries static, useful for people with
"normal" Linux machines but without GTK+.

  gcc -o foo foo.o bar.o -L/usr/lib -L/usr/X11R6/lib \
    -Wl,-Bstatic -lgtk -lgdk -lglib \
    -Wl,-Bdynamic -lXi -lXext -lX11 -lm


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Department
of Electrical Engineering, Faculty of Information Technology and Systems,
Delft University of Technology, PO BOX 5031,  2600 GA Delft, The Netherlands
Phone: +31-15-2785859  Fax: +31-15-2781843  Email J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]