Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can any library be reduced to a header file?

35 views
Skip to first unread message

Frederick Gotham

unread,
Jan 6, 2020, 10:09:45 AM1/6/20
to
Let's say my library is one source file and one header file. It contains one global variable with external linkage, and one function with external linkage, as follows:

// Contents of gotham.hpp

extern int Func(int);

extern int g_index;

// Contents of gotham.cpp

int g_index = 2;

int Func(int const i)
{
return i / 2;
}

I can combine this library into one header file like this:

// Contents of gotham.hpp

inline int &Func_Containing_gIndex(void)
{
static int s_index = 2;

return g_index;
}

static int &g_index = Func_Containing_gIndex();

inline int Func(int const i)
{
return i / 2;
}

To what extent can any library be reduced to one header file? One thing I see so far: This won't work if either the value of "g_index" or the address of "g_index" is required as a constexpr.

Frederick Gotham

unread,
Jan 6, 2020, 10:47:26 AM1/6/20
to
On Monday, January 6, 2020 at 3:09:45 PM UTC, Frederick Gotham wrote:

> inline int &Func_Containing_gIndex(void)
> {
> static int s_index = 2;
>
> return g_index;
> }


That should be:

return s_index;

Öö Tiib

unread,
Jan 6, 2020, 11:26:38 AM1/6/20
to
On Monday, 6 January 2020 17:09:45 UTC+2, Frederick Gotham wrote:
>
> To what extent can any library be reduced to one header file?

Fully. As of C++17 "The ​inline specifier can be applied to variables as
well as to functions."
And inline only means that it may be defined in header without ODR
(one definition rule) violations.

Alf P. Steinbach

unread,
Jan 6, 2020, 2:13:25 PM1/6/20
to
On 06.01.2020 16:09, Frederick Gotham wrote:
> Let's say my library is one source file and one header file. It
> contains one global variable with external linkage, and one function
> with external linkage, as follows:
>
> // Contents of gotham.hpp
>
> extern int Func(int);
>
> extern int g_index;
>
> // Contents of gotham.cpp
>
> int g_index = 2;
>
> int Func(int const i) { return i / 2; }
>
> I can combine this library into one header file like this:
>
> // Contents of gotham.hpp
>
> inline int &Func_Containing_gIndex(void) { static int s_index = 2;
>
> return g_index; }
>
> static int &g_index = Func_Containing_gIndex();
>
> inline int Func(int const i) { return i / 2; }
>
> To what extent can any library be reduced to one header file?

To the extent that its implementation doesn't have to use dirty headers
(e.g. `<windows.h>` comes to mind) that one doesn't want to expose, that
one needs the possibility to distribute updates to object code without
client code having to be recompiled, to the extent that company or
project rules permit header only modules, and to the extent that one
doesn't need to keep the source unavailable in order to protect of
intellectual property rights.

I grabbed some of that wording from <url:
https://accu.org/index.php/journals/482>, an old article that can be
interesting now just because some of the issues discussed in earnest
then are now so irrelevant, i.e. how we have progressed.

Anyway, in some cases one can work around the header dependency issue.
E.g. for the mentioned header, with 64-bit programming it's practically
doable, but inconvenient, to just declare in ones own headers the API
functions that one uses. Since there is a single common machine code
level calling convention, and smarter linkers now than 20 years ago,
there is little or no chance of one's tools fouling it up, so I've done
it a number of times -- but only in exploratory, experimental code.


> One thing I see so far: This won't work if either the value of
> "g_index" or the address of "g_index" is required as a constexpr.

But then neither would the original.


- Alf

James Kuyper

unread,
Jan 6, 2020, 10:59:04 PM1/6/20
to
It also won't work if the source code for the library is not C++. For
example, assembly code.
0 new messages