On 02/14/2010 03:28 PM, David Haller wrote:
Hello,
On Sun, 14 Feb 2010, Dave Plater wrote:
Meanwhile, before I start searching for static lib packaging policies, is building blender against the static libraries and including them in the blender rpm ok?
You don't need to. Linking with a static library includes all needed objects from the lib in the binary, just as any other object. Here's an example (I have a small "utility lib" of my own):
$ ar t ~/lib/libdhaller.a xmalloc.o xexit.o dhtextutil.o sumiton.o xreadline.o hexdump.o xstrftime.o itoa.o llseek.o mkdtemp.o
Now, let's create a program that uses something from that lib:
$ cat <<EOF > linkingdemo.c #include
int main(void) { int sum = sum0ton(8); return sum; } EOF Now build dynamically:
$ set -x $ gcc -Wl,-t -o linkingdemo linkingdemo.c $(dhaller-config --cflags --libs) ++ dhaller-config --cflags --libs + gcc -Wl,-t -o linkingdemo linkingdemo.c -I/home/dh/include -L/home/dh/lib -ldhaller /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/../../../../i686-pc-linux-gnu/bin/ld: mode elf_i386 /usr/lib/crt1.o /usr/lib/crti.o /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/crtbegin.o /tmp/ccSgP8ha.o -ldhaller (/home/dh/lib/libdhaller.so) -lgcc_s (/opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libgcc_s.so) /lib/libc.so.6 -lgcc_s (/opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libgcc_s.so) /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/crtend.o /usr/lib/crtn.o
Now, the binary is dynamically linked against libdhaller.so. Let's check that:
$ nm linkingdemo | grep sum U sum0ton
Our demo-program _USES_ one function.
And now, let's link statically (only against libdhaller):
$ gcc -Wl,-t -o linkingdemo linkingdemo.c -Wl,-Bstatic $(dhaller-config --cflags --libs) -Wl,-Bdynamic ++ dhaller-config --cflags --libs + gcc -Wl,-t -o linkingdemo linkingdemo.c -Wl,-Bstatic -I/home/dh/include -L/home/dh/lib -ldhaller -Wl,-Bdynamic /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/../../../../i686-pc-linux-gnu/bin/ld: mode elf_i386 /usr/lib/crt1.o /usr/lib/crti.o /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/crtbegin.o /tmp/cceh9bpI.o (/home/dh/lib/libdhaller.a)sumiton.o -lgcc_s (/opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libgcc_s.so) /lib/libc.so.6 -lgcc_s (/opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/libgcc_s.so) /opt/gcc/3.3.5/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.5/crtend.o /usr/lib/crtn.o
As you can see in the middle, only 'sumiton.o' gets pulled from libdhaller.a and linked together with the linkingdemo.o (here: /tmp/cceh9bpI.o), and the usual startup stuff and libgcc/libc.
The sumiton.o is an _integral_ part of the resulting binary. Let's check with 'nm':
$ nm linkingdemo | grep sum 08048510 T sum0ton 080484c0 T sumiton
There are (basically) only those two functions in sumiton.o. And our demo-program _CONTAINS_ both functions, the .o from the library was included just like any of "our" object files (here the linkingdemo.o called /tmp/cceh9bpI.o as of gcc magic using tempfiles ;)
So, the conclusion is: Just link statically against those libs (easiest way is just putting the lib in the gcc-call, see below) and your binary _will contain_ the libs. Sample (without the -Wl,-t stuff, it's the same as above):
$ gcc $(dhaller-config --cflags) -o linkingdemo linkingdemo.c ~/lib/libdhaller.a
Remember: you need to put the libs _after_ the code that uses them on the commandline. If I reverse linkingdemo.c and the lib I get:
$ gcc $(dhaller-config --cflags) -o linkingdemo ~/lib/libdhaller.a linkingdemo.c /tmp/cc0cyuLi.o: In function `main': linkingdemo.c:(.text+0x20): undefined reference to `sum0ton' collect2: ld returned 1 exit status
(linkingdemo.c plays the role of the .o object file as well here).
Recap for linking statically:
a) ... OBJECTS_THAT_USE_libfoo ${PATH}/libfoo.a ...
b) ... OBJECTS_THAT_USE_libfoo -Wl,-Bstatic -L${PATH} -lfoo -Wl,-Bdynamic ...
Where ${PATH} may be a relative or absolute path to the lib.
BTW: yes, -Wl,-t (i.e. '-t' option to 'ld') is at times extremely helpful tracking down linking errors :) And "-Wp,-H" is the corresponding option for tracking Header-inclusion ;)
HTH, -dnh
Thanks for the comprehensive explanation, static libs it is for now. Dave P -- To unsubscribe, e-mail: opensuse-packaging+unsubscribe@opensuse.org For additional commands, e-mail: opensuse-packaging+help@opensuse.org