简体   繁体   中英

Why does the boost lib have external linkage, not internal? (Best practice of linkage for header-only)

Boost library defines functions as following style ;

namespace boost::io::detail {
    template<class Iter, class Facet>
    Iter wrap_scan_notdigit(...) {
        // snip
    }
}

To my understanding, the function has external linkage because

  • the namespace is on the global scope, not unnamed
  • function is not "static" declared

From my experience, the external linkage on the header often causes the ODR violation. For example, two different translation units could include the different versions; this often happens when you use 3rd-party.so .a (static) libs.

The critical issue of the ODR violation is NDR: compiler/linker doesn't have to emit errors, but it could generate an execution-time bug in the rare case.

The internal linkage can avoid the problem.

So my question is (same as on the title):

  • Why is the boost's linkage external, not internal?
  • (To be generalized above) How should I set the linkage for my header-only library?

the external linkage on the header often causes the ODR violation.

Only when the headers are poorly written (or possibly when they are poorly used, if definitions depend on preprocessor directives).

External linkage reduces code bloat, as you need only one copy of each function per program, rather than one copy per translation unit.

this often happens when you use 3rd-party.so libs.

Library object code should not export symbols from header-only libraries. The point of header-only is that it is available without linking to library object code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM