At Mon, 7 Jul 2025 15:49:01 +0200, Stephan <stephan...@googlemail.com> wrote:
Subject: Re: Import .a file into another library
>
> I am compiling a library on NetBSD using a Makefile that I have made
> which is based on <bsd.lib.mk>. It depends on some other libraries
> from base or pkgsrc - so far so good.

If a library depends on another library, normally this dependency is
specified when linking the final program, not when creating the library.

Especially for normal static libraries.

> However, there is one dependency to math/mapm which only comes with a
> .a file and provides no .so file. I had included -lmapm in LDFLAGS
> already, but according to nm the library seems to have all references
> marked with U in the symbol table, so I guess it was not imported.

While it is possible to incorporate the objects from one static library
into another, this is not normally desirable and should be avoided.

Normally one does actually want the final library to still depend on the
other library(ies) regardless of whether they are static or dynamic.
I.e. you're getting what you should get!


> Benny wrote:
> >
> > AIUI, you just name .a files on the command line, like any .o file.

While that works, that's not the normal way.  Use "-l" (and maybe "-L").


> After more experimentation I managed to get it working with the
> following statements:
>
> LIBDPLIBS+= mapm /usr/pkg/lib
> LIBDO.mapm = _external

LIBDPLIBS and LIBDO.<lib> have some unnecessary effects and are not
really intended for this use where static libraries are being linked.

They are intended for depending on un-installed libraries still within
their source subdirectories when linking them to a shared library, and
primarily for private libraries, not externally installed libraries,
despite the magic "_external" tag.

These settings will not have the effect you think they will, especially
not for any static library that is created.  For one it misses adding
the "-L"!

See the documentation in /usr/share/mk/bsd.README

However beware that <bsd.lib.mk> (as with all the system MK files) is
designed primarily for building things integrated within the main system
source directory.


When building independent third-party libraries with BSD make one must
make several adjustments to coerce the system MK files to work nicely.
I have a number of example projects where I've done this, though the
best example is not yet published (my initial trials were for YAJL, see
my version on github, but note it may be more complex than strictly
necessary).


So, as I mentioned above, one normally must specify all the libraries
when linking a final program, even if some of those libraries are only
dependencies of other libraries.  Many people forget to specify all
inter-library dependent libraries, but that causes chaos and headaches
for static linking.  It's not hard to get it right and prevent all those
problems, but usually the only way to be sure, especially for
inexperienced developers, is to actually test a full static link.  If
you don't test full static linking you may have mysteries and unexpected
results even when things appear to be working.  For one, C doesn't have
namespaces!  Some objects may not be used the way you want them to be
used!

So normally in the makefile for a program (i.e. the makefile including
<bsd.prog.mk>) which uses libyourlib which also depends on libmapm:

        LDADD += -L/usr/pkg/lib -lyourlib
        LDADD += -L/usr/pkg/lib -lmapm          # dependency of yourlib

This will then work to link the program either statically or
dynamically, as desired, with both libraries (after those libraries are
both installed, of course).

--
                                        Greg A. Woods <gwo...@acm.org>

Kelowna, BC     +1 250 762-7675           RoboHack <wo...@robohack.ca>
Planix, Inc. <wo...@planix.com>     Avoncote Farms <wo...@avoncote.ca>

Attachment: pgpmOBKNjZDjw.pgp
Description: OpenPGP Digital Signature

Reply via email to