Benches macOS

5

bzip2: makefile problem?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi to all,

I have the following problem: I'm trying to compile bzip2 1.0.3 from sources.

with minor modifications to the makefile, it succeeds, but the (fresh) bzip2 executable returns "version 1.0.2", even if in the binary file libbz2.a the string is 1.0.3.

I noticed that there is a libbz2.dylib in the path, and my timid guess is that the OS is invoking its own library anyway.

Can somebody point me in the right direction?

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    Daniel Macks

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Static libs (.a files) are actually embedded into a binary when the binary is compiled. Dynamic libs (.dylib files) are loaded when a binary is run; the path to the dylib are embedded into the binary at compile-time. Sounds like when you compiled your bzip2 executable, the compiler may have seen the system's .dylib not your own .a when it looked for "some libbz2 file".

  2. 0

    m.holmes

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Yes, that's exactly the problem, but the makefile looks correct:

    bzip2: libbz2.a bzip2.o

    $(CC) $(CFLAGS) $(LDFLAGS) -v -o bzip2 bzip2.o -L. -lbz2

    CC is gcc, CFLAGS (compiler flags) are optimization related, so they don't interfere; LDFLAGS is empty.

    -L. means "look for libraries here" and it's ok.

    -lbz2 should look for libbz2.a in directory ".", but if I modify the makefile NOT to build libbz2.a before bzip2 (and delete libbz2.a), IT WORKS ANYWAY.

    Adding -verbose to gcc outputs:

    gcc -Wall -Winline -O -g -D
    FILE_OFFSETBITS=64 -v -o bzip2 bzip2.o -L. -lbz2

    [...]

    /usr/libexec/gcc/powerpc-apple-darwin8/4.0.0/collect2

    -dynamic

    -arch ppc -weak
    referencemismatches non-weak -o bzip2 -lcrt1.o /usr/lib/gcc/powerpc-apple-darwin8/4.0.0/crt2.o -L. -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0 -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0 -L/usr/lib/gcc/powerpc-apple-darwin8/4.0.0/../../.. bzip2.o -lbz2 -lgcc -lgcc_eh -lSystemStubs -lmx -lSystem

    There's a -dynamic in the middle.

    So, what should I do?

    I'll try renaming

    /usr/lib/libbz2.1.0.2.dylib

    /usr/lib/libbz2.1.0.dylib

    /usr/lib/libbz2.dylib

    then recompiling, then restoring the dylibs

  3. 0

    Daniel Macks

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ayup...here's an interesting flag from the "ld" manpage that may solve your problem:

    <dl><dt>-search
    pathsfirst</dt><dd>By default when the -dynamic flag is in effect, the -lx and -weak-lx options first search for a file of the form `libx.dylib' in each directory in the library search path, then a file of the form `libx.a' is searched for in the library search paths. This option changes it so that in each path `libx.dylib' is searched for then `libx.a' before the next path in the library search path is searched.</dt></dl>

    Alternately, you could take fink's approach: hack Makefile to build libbz2 as a shared library instead of a static one. Of course then you'd have to install your libbz2*.dylib also instead of having a stand-alone bzip2 binary...

    Edit: fixed formatting

  4. 0

    m.holmes

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks for your help.

    in fact temporarily removing the dylib worked fine, but the -search
    pathsfirst option will turn useful...

    I had a quick look at the manpage: the second option you mention would simply imply adding -dylib to the linker options, wouldn't it?

    (of course, then I should also modify the install section, but that's unrelated)

  5. 0

    Daniel Macks

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Shared libraries can be a pain to hand-compile because you have to tell them where they will be installed ("where am I and what is my official path, really?" gets hard-coded into a .dylib). So you might do
    gcc -dynamiclib $(OBJS) -install_name $(PREFIX)/lib/libbz2.dylib -o libbz2.dylib

    Here's fink's patchfile for bzip2:

    http://cvs.sf.net/viewcvs.py/fink/dists/10.4-transitional/unstable/main/finkinfo /base/bzip2.patch

    which is used with the usual
    make PREFIX=/wherever && make install PREFIX=/wherever