C++ Logo


Advanced search

[std-proposals] interlibrary Vs intralibrary

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 19 Apr 2023 23:38:25 +0100
Typically when we're writing a program and we want to link with a 3rd
party library, we have the option of linking statically or dynamically
with the library.

Let's say we have a library called libspellcheck and that it contains
a global variable called 'status'. This variable is not intended to be
accessed from outside the library, however it is accessed from about
two dozen translations units within the library. For this reason,
'status' is declared and defined as 'extern':

    extern int status;

If we link dynamically with libspellcheck, the global variable
'status' won't be made available to us. However if we link statically,
we can access all the global variables.

Compilers have different ways of specifying that a variable/function
is accessible from outside the library, for example -fvisibility or
dllexport or --version-script. I don't know of any compiler though
that has an option for making static libraries that says "make this
symbol visible within the library but invisible outside the library".

We already have the keyword 'extern' to specify external linkage. I
think that there can be considered to be two subcategories of extern:
(a) visible to translation units inside the library, and also visible
to translation units outside the library
(b) visible to translation units inside the library, but not to
translation units outside the library

The first one could be:

    extern int status;

and the second one could be:

    intern int status;

So overall this would give us three kinds of linkage:

    static int status; // not visible outside this translation unit
    extern int status; // visible everywhere
    intern int status; // visible to other translation units inside the library

Received on 2023-04-19 22:38:38