Re: Static linking



Tristan Van Berkom wrote:
Johannes Tanzler wrote:

On Tue, 2003-09-30 at 17:56, Russell Shaw wrote:
...

How do i get "pkg-config gtk+-2.0 --libs" to put out -static
instead of --export-dynamic ?

Try
 `pkg-config --libs gtk+-2.0` -static

The explicit "-static" overrides "--export-dynamic".

I figured out: pkg-config --libs-only-l gtk+-2.0
then i can add whatever i want externally.


hmmm,
       --export-dynamic [ is an option to ld ]
       -static                   [ is a linker option to gcc ]

Usualy object files are linked according to the preceeding option passed,
i.e.:

gcc -o prog  prog.c  -shared -lone -static -ltwo

will create a program out of prog.o and libtwo.a
that links dynamicly with the object file `libone.so'

man ld seems to say -shared is for *creating* a shared object (P.429 Running
Linux, 3rd Ed). Maybe you mean -Bdynamic.

Are you sure that gcc doesn't pass `--export-dynamic' to the linker if it
finds `-static' on its command line ?

I could use some enlightenment on this subject ;-)

--export-dynamic is to make available all the symbols from an object rather
than just the ones that have been referenced by another object during the link.

I found that to link a gtk app statically, you have to find the X libs
that contain the symbols referenced by libgtk-x11-2.0.a and libgdk-x11-2.0.a.

I linked with a mixture of static and dynamic libs:

gcc -g -O0 -o shapegen main.o gui.o xmalloc.o object.o menu.o dialog.o
    -Wl,/usr/lib/libgtk-x11-2.0.a /usr/lib/libgdk-x11-2.0.a -latk-1.0
    -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0
    /usr/lib/libgobject-2.0.a -lgmodule-2.0 -ldl /usr/lib/libglib-2.0.a -lc
    -L /usr/X11R6/lib -lXinerama -lXi

It seems that static .a libraries can only be found by using the explicit
path if you are linking a mixture of static and dynamic libraries, because
specifying -static anywhere in the linker options seems to make *everything*
static even if you have -Bdynamic for some libraries (gcc 3.3.1, ld 2.14.90.0.4).

Is this a bug, or should -static apply globally?


To quickly find which X lib has a symbol:

  nm -o `ls /usr/X11R6/lib/*` 2>/dev/null | grep "XineramaQueryScreens"

or a script:

#!/bin/sh
# Input parameter is Xlibs symbol to find
list=`ls /usr/X11R6/lib/*`
for i in $list ; do
    nm "$i" 2>/dev/null | grep -qs "$1" && echo "$i"
done




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