Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell compiler to NOT optimize certain code away?
    text
    copied!<p>Is there a way to tell the compiler (g++ in my case) to <strong>not</strong> optimize certain code away, even if that code is not reachable? I just want those symbols in the object file.</p> <p>Example: Here is a simple function, and I do want this function to be compiled, even if it's never called.</p> <pre><code>void foo(){ Foo&lt;int&gt; v; } </code></pre> <p>If there is no official compiler directive, is there a trick to make the compiler think that it's an important function? Or at least make it think that it can't safely be ignored? I tried something like this:</p> <pre><code>extern bool bar; void foo(){ if(bar){ Foo&lt;int&gt; v; } } </code></pre> <p>but that didn't seem to do it.</p> <p>(If you really want to know why I on earth would want that -- it has to do with <a href="https://stackoverflow.com/questions/555330/templates-use-forward-declarations-to-reduce-compile-time">this</a> question, where, instead of explicit template instantiation with <code>template class Foo&lt;int&gt;</code> I simply want to be able to write <code>Foo&lt;int&gt; v</code>, since in many cases that's easier since it implicitly instantiates all functions needed, and it does work fine in debug mode without optimizations ...)</p> <p><strong>UPDATE:</strong></p> <p>Here is what I want to do (as a compilable mini example):</p> <p><strong>foo.h</strong> (such files are given to me and not changeable)</p> <pre><code>template&lt;class T&gt; struct Foo { T val_; Foo(T val) : val_(val) { // heavy code, long compile times } }; </code></pre> <p><strong>foo-instantiation.cpp</strong></p> <pre><code>#include "foo.h" void neverCalled() { Foo&lt;int&gt; f(1); } // The standard way to instantiate it is this: // template class Foo&lt;int&gt;; // but in reality it is often hard to find out // exactly what types I have to declare. // Usage like Foo&lt;int&gt; f(1); will instantiate all // dependent types if necessary. </code></pre> <p><strong>foo-decl.h</strong> (an interface that I extracted from foo.h)</p> <pre><code>template&lt;class T&gt; struct Foo { T val_; Foo(T val); // no heavy code, can include anywhere and compile fast }; </code></pre> <p><strong>main.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include "foo-decl.h" int main(int argc, char** argv){ Foo&lt;int&gt; foo(1); return 0; } </code></pre> <p><strong>Compilation (no optimization)</strong></p> <pre><code>g++ -c main.cpp g++ -c foo-instantiation.cpp g++ main.o foo-instantiation.oo </code></pre> <p><strong>Compilation (optimization)</strong></p> <pre><code>g++ -O2 -c main.cpp g++ -O2 -c foo-instantiation.cpp g++ main.o foo-instantiation.oo main.o(.text+0x13): In function `main': : undefined reference to `Foo&lt;int&gt;::Foo(int)' collect2: ld returned 1 exit status </code></pre> <ul> <li>I tried precompiled headers instead, but the template instantiation method makes for much faster compilation.</li> <li>Compiling <code>foo-instantiation.cpp</code> without optimization is not so ideal because then the library code (<code>foo.h</code> and others) will run slower.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload