title: How to declare a global const initialized by a function in the header?
body: <p>I have an older source code, like this, in a header used from many places in my project:</p>
  
  <pre><code>const int myVar = myFunc();
  </code></pre>
  
  <p>What I want:</p>
  
  <ul>
  <li>Being a global, const variable, I would like if it would be linked only once in the binary.</li>
  <li>Thus, also <code>myFunc()</code> should be called only once in the global variable initialization phase.</li>
  </ul>
  
  <p>Now the problem is that I get this warning from the <code>.cc</code> I compile:</p>
  
  <pre><code>In file included from mySource.cc:7:0:
  myHeader.h:59:11: warning: ‘myVar’ defined but not used [-Wunused-variable]
   const int myVar = myFunc();
         ^
  </code></pre>
  
  <p>Note, <code>mySource.cc</code> really doesn't use <code>myVar</code>, thus the warning is okay, but other sources yes.</p>
  
  <p>I think, the best would be if I would declare <code>myVar</code> only in the header, some like so:</p>
  
  <p>myHeader.h:</p>
  
  <pre><code> int myVar;
  </code></pre>
  
  <p>mySource.cc:</p>
  
  <pre><code> int myVar = myFunc();
  </code></pre>
  
  <p>But in this case, I can't declare it as const. <em>This variable should be a const.</em> Yes, I know it will be on a writable memory page, only the c++ will see it as a constant, but this is exactly what I want.</p>
  
  <p>Thus, I also want to avoid this warning. Furthermore, I think <code>myFunc()</code> would be called many times, what I don't want.</p>
  
  <p>How can I do this?</p>