title: Is there a standard way to ensure that a piece of code is executed at global scope?
body: <p>I have some code I want to execute at global scope. So, I can use a global variable in a compilation unit like this:</p>
  
  <pre><code>int execute_global_code();
  namespace {
  int dummy = execute_global_code();
  }
  </code></pre>
  
  <p>The thing is that if this compilation unit ends up in a static library (or a shared one with <code>-fvisibility=hidden</code>), the linker may decide to eliminate <code>dummy</code>, as it isn't used, and with it my global code execution.</p>
  
  <p>So, I know that I can use concrete solutions based on the specific context: specific compiler (pragma include), compilation unit location (attribute visibility default), surrounding code (say, make an dummy use of <code>dummy</code> in my code).</p>
  
  <p>The question is, is there a standard way to ensure <code>execute_global_code</code> will be executed that can fit in a single macro which will work regardless of the compilation unit placement (executable or lib)? ie: only standard c++ and no user code outside of that macro (like a dummy use of <code>dummy</code> in <code>main()</code>)</p>