title: Unused C++ static member functions/variables not optimized out
body: <p>I have enabled compiler and linker optimization in the hopes of removing all unused code/data from my ARM32 executable. From my map file, I can see that the unused sections of code were indeed discarded so the optimization flags are mostly working, except for static member functions/variables of an unused class. Any ideas on how to get rid of this would be really appreciated, because this adds up to quite a bit on a resource-constrained embedded platform!</p>
  
  <p>Here's an MVCE compiled using g++ 7.5. on Ubuntu 18.04</p>
  
  <pre><code>#include &lt;string&gt;
  
  #include &lt;iostream&gt;
  #include &lt;string.h&gt;
  
  class unusedClass {
  public:
    unusedClass() {};
    ~unusedClass() {};
    static std::string className;
  
    void initArray(void) {
      memset(a, 0, sizeof(a));
    }
  
    void printArray(void) {
      for (auto&amp; i:a) {
        std::cout&lt;&lt; i &lt;&lt; std::endl;
      }
    }
  
    static void printClassName(void) {
      std::cout &lt;&lt; "This is a static member function of the class \"UNUSED CLASS\""&lt;&lt; std::endl;
    }
  
  private:
    int a[1000];
  };
  
  std::string unusedClass::className = "unusedClass";
  
  int main() {
    std::cout &lt;&lt; "Running the Clean-up Dead Code Test" &lt;&lt; std::endl;
    return 0;
  }
  
  
  </code></pre>
  
  <p>Compile with optimization flags to remove unused code</p>
  
  <pre class="lang-sh prettyprint-override"><code>g++ -Os -flto test.cpp
  </code></pre>
  
  <p>To check if the static member variable was compiled into the executable</p>
  
  <pre class="lang-sh prettyprint-override"><code>readelf -a --wide a.out | awk '$4 == "OBJECT" { print }'
  </code></pre>
  
  <pre class="lang-sh prettyprint-override"><code>29: 0000000000201140    32 OBJECT  LOCAL  DEFAULT   24 _ZN11unusedClass9classNameB5cxx11E
  </code></pre>