CppCon 2017: Nir Friedman “What C++ developers should know about globals (and the linker)”

CppCon · 51K views · 2017 · 35:03

Open on YouTube

C, C++, Rust, Go & systems languagesCompilers, interpreters & program analysistalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining how C++ globals, inline variables, initialization order and static vs shared library linking actually behave at link time.

Recommended by 2 people across 2 different months, 2017 to 2020.

What it is

A CppCon 2017 conference talk by Nir Friedman on the pitfalls of global variables in C++ and how the linker affects them. It uses nm and objdump to show how tiny valid programs can segfault with the gcc linker, covers eager vs lazy and private vs public globals, and presents safe idioms including inline variables.

What people said

Recommends Philipp Claßen · 2017-11 · Stack Overflow · on “How do inline variables work?”

Inline variables are very similar to inline functions. It signals the linker that only one instance of the variable should exist, even if the variable is seen in multiple compilation units. The linker needs to ensure that no more copies are created. Inline variables can be used to define globals in header only libraries. Before C++17, they had to use workarounds (inline functions or template hacks). For instance, one workaround is to use the Meyers’ singleton [LINK] with an inline function: inline T& instance() { static T global; return global; } There are some drawbacks with this approach, mostly in terms of performance. This overhead could be avoided by template solutions, but it is easy t

Recommends phön · 2018-07 · Stack Overflow · on “Alternative id generators for types”

Its well defined for different translation units. The question which remains is whether its defined for mixing and using an inline variable in a shared and/or static library and the executeable or not. When watching this talk ( [this video] ), i get the impression that its just well defined and just works. Inline comes into play @ the time marked. But you would probably need to watch a bit more upfront to get the context.

Cites monkey0506 · 2018-07 · Stack Overflow · on “How do inline variables work?”

@Cheersandhth.-Alf Nir Friedman says [this video] that `inline` variables in a header file will always end up being constructed in the correct order because the header they are defined in must be included before they can be referenced, and therefore any given translation unit will include those variables in the correct, necessary construction order. If header "B.h" depends on a global in "A.h", then "B.h" must `#include` "A.h" itself in order to reference the global, and so by the time "B.h" is included in an actual translation unit, the globals are properly ordered.

Cites Adam Stepniak · 2020-07 · Stack Overflow · on “Why linker does not complain about multiple definitions when linking against shared and static”

I've encountered recently on a problem with static initialization fiasco in the code that I was developing. It made me realize my lack of knowledge about linking process and global variables initialization so I've found this this [this video] very interesting talks at cppcon about globals, linking and possible issue with having static library embedded in shared and linking both at the same time (with globals defined). What was basically said there is that when having following structure: //static.hpp #pragma once #include <string> extern std::string globalString; //static.cpp #include "static.hpp" std::string globalString; //shared.hpp #pragma once #include "static.hpp" #include <string> std::str