|
Boost : |
Subject: Re: [boost] C++03 / C++11 compatibility question for compiled libraries
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2018-02-12 23:21:08
On 12/02/2018 21:45, Raffi Enficiaud wrote:
>> A function that has the same mangled name in
>> different standards and is defined in multiple
>> translation units (i.e. inline functions or
>> function templates) must be identical regardless
>> of the standard.
>
> Do you know if at least the linker emits an error or a warning if, same
> symbol exists but with different definition? I really have not a single
> clue, but it seems that this is a grey area where people just "hope" as
> you said and have little control.
No. If two things with different definitions resolve to different
mangled names, then the linker can detect that as a missing reference
and will report it as such (which is why most attempts to keep these
sorts of things sane rely on inline namespaces, where the "real" name
visible to the linker has extra decoration that the programmer doesn't
see. Inline namespaces themselves aren't a thing in C++03, but you can
still do the equivalent).
Two symbols with the same mangled name are typically treated as
identical by the linker regardless of anything else, and it's free to
match them up and throw away multiple definitions without any diagnostic
at all. Hence why ODR violations can be a significant problem, and one
that is hard to track down.
(Header-only and highly templated libraries can be the worst offenders
in this regard: it only takes one translation unit compiled with
slightly different settings to break seemingly unrelated code, sometimes
apparently at random if the linker picks different ones to keep each
time. Compiled libraries at least have the option of supplying both
C++03 and C++11 implementations in separate namespaces, though it can
become a game of whack-a-mole to enumerate all the different
ABI-breaking settings that you want to concurrently support.)
In an ideal world, the linker would compare the *size* of the symbols
and issue a diagnostic if they don't match, as this is highly suspicious
of an ODR issue. However I've yet to encounter one in the wild that
actually does this. (And there are certain kinds of problems it
wouldn't detect, but it would help with the majority of ODR violations.)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk