I'm afraid you'll need to modify your linkage to achieve this.
You should be clear that the entities within an archive (.a
)
that can be linked, or not linked, with your executable are not
individual symbol definitions but individual archive members,
which are object files, each of which may define arbitrarily
many symbols. If you want to link a symbol from an archive, you link the whole
archive member that defines it.1
The foremost difference between linking an archive (.a
)
with your executable and linking an object file (.o
) is that
an object file will be linked, unconditionally, whereas an archive member
will be linked only if it provides a definition for at least one symbol that
has been referenced but not defined when the archive is examined.
Hence the ordinary, non-roundabout way of ensuring that a symbol foo
is linked
unconditionally is to link an object file that defines it. There is no
way to mark an archive member foo.o
as must be linked because, if you
must link foo.o
, you do it by linking foo.o
.
So if foo
resides in an archive member foo.o
, you can
extract that member from the archive:
ar x libthing.a foo.o
and add foo.o
to your linkage, even if you don't have the foo
source
from which to compile foo.o
.
If there are a lot of functions that you want to link unconditionally
then you might either compile them all from source into a single object
file, if you have the source, or you might collect all the object files
that define them into a single archive that you link with --whole-archive
.
I was hoping to find a way to advertise the public nature of the symbol in the library itself
For linkage purposes, there are nothing but public symbols in a library: by
definition any symbols that the linker can see are public.
[1] If you are in a position to compile an object file that goes into to
your linkage, then by the use of appropriate compiler and linker flags you
can ensure that redundant symbols it may contribute are finally discarded by
the linker. See
this answer