Erik Gustavsson ([EMAIL PROTECTED]) wrote:
> This may be semi-offtopic because its more like a general Unix/Linux
> question... How can I compile libmp3lame as a shared library
> (libmp3lame.so) ??
This will vary from one system to another.
> Can this be added as an alternative target in the
> Makefile perhaps? Or even made the default for Linux?
Since you're using Linux I can give you an answer. It may not be useful
on other system types, though.
Also, I'll assume you're using a relatively recent version of a Linux
distribution (one that has glibc 2.1.x). This won't work on libc5 (you
would need to recompile the object files with -fPIC) and I'm not sure
about glibc 2.0.x. Therefore everything in this message is presented
on an "at-your-own-risk" basis. FYI, my computer is an i386 running
Debian 2.2.
First you should read the ELF HOWTO. It's outdated, but it's the best
one-page primer on building shared libs in Linux that I've yet seen. The
relevant section is <http://www.telent.net/linux/ELF-HOWTO-3.html#ss3.2>.
Now, because this is a one-shot deal, I'm going to cheat a bit.
Basically, what I'll do is build everything, then delete the things I
want to rebuild with shared libs. Then I'll ask "make" how it would
go about building them normally (static libs), and modify the resulting
commands to use shared libs instead.
Specifically, below, I changed "ar cr libmp3lame.a" to "gcc -shared
-Wl,-soname,libmp3lame.so.3.86 -o libmp3lame.so.3.86" on the compile
command, and then I changed "-L." to "-L/usr/local/lib" on the linker
command. The rest is cut-and-pasted from the "make -n" output (but of
course I don't need to run ranlib at all).
Note also that I had to add /usr/local/lib to my /etc/ld.so.conf file so
that /usr/local/lib would be searched for shared libraries. This may
or may not already be done on your system. If "ldd" doesn't find your
shared libmp3lame, then you'll need to edit ld.so.conf and rerun ldconfig.
dwarf:~/lame3.86$ make
[...]
dwarf:~/lame3.86$ rm libmp3lame.a lame
dwarf:~/lame3.86$ make -n
ar cr libmp3lame.a brhist.o bitstream.o fft.o get_audio.o id3tag.o ieeefloat.o lame.o
newmdct.o parse.o portableio.o psymodel.o quantize.o quantize-pvt.o vbrquantize.o
reservoir.o tables.o takehiro.o timestatus.o util.o vorbis_interface.o VbrTag.o
version.o mpglib/common.o mpglib/dct64_i386.o mpglib/decode_i386.o mpglib/layer3.o
mpglib/tabinit.o mpglib/interface.o mpglib/main.o
ranlib libmp3lame.a
gcc -O3 -fomit-frame-pointer -funroll-loops -ffast-math -finline-functions -Wall -o
lame main.o gtkanal.o gpkplotting.o -L. -lmp3lame -lm `gtk-config --libs`
-lncurses -L/usr/local/lib -lvorbis
dwarf:~/lame3.86$ gcc -shared -Wl,-soname,libmp3lame.so.3.86 -o libmp3lame.so.3.86
brhist.o bitstream.o fft.o get_audio.o id3tag.o ieeefloat.o lame.o newmdct.o parse.o
portableio.o psymodel.o quantize.o quantize-pvt.o vbrquantize.o reservoir.o tables.o
takehiro.o timestatus.o util.o vorbis_interface.o VbrTag.o version.o mpglib/common.o
mpglib/dct64_i386.o mpglib/decode_i386.o mpglib/layer3.o mpglib/tabinit.o
mpglib/interface.o mpglib/main.o
dwarf:~/lame3.86$ su
Password:
dwarf:/home/greg/lame3.86# cp libmp3lame.so.3.86 /usr/local/lib
dwarf:/home/greg/lame3.86# ln -s libmp3lame.so.3.86 /usr/local/lib/libmp3lame.so
dwarf:/home/greg/lame3.86# ldconfig
dwarf:/home/greg/lame3.86# exit
exit
dwarf:~/lame3.86$ gcc -O3 -fomit-frame-pointer -funroll-loops -ffast-math
-finline-functions -Wall -o lame main.o gtkanal.o gpkplotting.o -L/usr/local/lib
-lmp3lame -lm `gtk-config --libs` -lncurses -L/usr/local/lib -lvorbis
dwarf:~/lame3.86$ ls -l lame /usr/local/lib/libmp3lame.so*
lrwxrwxrwx 1 root staff 18 Sep 10 20:36 /usr/local/lib/libmp3lame.so
-> libmp3lame.so.3.86*
-rwxr-xr-x 1 root staff 789699 Sep 10 20:36
/usr/local/lib/libmp3lame.so.3.86*
-rwxrwxr-x 1 greg greg 136353 Sep 10 20:36 lame*
dwarf:~/lame3.86$ ldd ./lame
libmp3lame.so.3.86 => /usr/local/lib/libmp3lame.so.3.86 (0x40018000)
libm.so.6 => /lib/libm.so.6 (0x40134000)
libgtk-1.2.so.0 => /usr/lib/libgtk-1.2.so.0 (0x40151000)
libgdk-1.2.so.0 => /usr/lib/libgdk-1.2.so.0 (0x40275000)
libgmodule-1.2.so.0 => /usr/lib/libgmodule-1.2.so.0 (0x402a9000)
libglib-1.2.so.0 => /usr/lib/libglib-1.2.so.0 (0x402ac000)
libdl.so.2 => /lib/libdl.so.2 (0x402ce000)
libXi.so.6 => /usr/X11R6/lib/libXi.so.6 (0x402d2000)
libXext.so.6 => /usr/X11R6/lib/libXext.so.6 (0x402da000)
libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x402e6000)
libncurses.so.5 => /lib/libncurses.so.5 (0x40388000)
libc.so.6 => /lib/libc.so.6 (0x403c6000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
> Is there a reason
> for *not* using shared libs?
Yes. For one, it's more complex. (On Linux/ELF, it's nearly trivial;
on something like HP-UX or AIX it's a bit harder than the above; and on
Linux/a.out, you might as well just forget it entirely.) Also, shared
libraries take a little bit longer to load than statically linked code.
Finally, the benefits of shared libraries (reduced disk space, and
reduced memory usage if multiple copies of the shared lib are in use
simultaneously) only occur if you link two or more programs with the
same shared library. Is this libmp3lame library really of use to any
program other than lame?
--
Greg Wooledge | "Truth belongs to everybody."
[EMAIL PROTECTED] | Red Hot Chili Peppers
http://www.kellnet.com/wooledge/ |
PGP signature