Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As other posters have indicated, the linker typically does not remove dead code before building the final executable. However, there are often Optimization settings you can use to force the linker to try extra hard to do this.</p> <p>For GCC, this is accomplished in two stages:</p> <ol> <li><p>First compile the data but tell the compiler to separate the code into separate sections within the translation unit. This will be done for functions, classes, and external variables by using the following two compiler flags:</p> <p>-fdata-sections -ffunction-sections </p></li> <li><p>Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):</p> <p>-Wl,--gc-sections</p></li> </ol> <p>So if you had one file called test.cpp that had two functions declared in it, but one of them was unused, you could omit the unused one with the following command to gcc(g++):</p> <pre><code>gcc -Os -fdata-sections -ffunction-sections test.cpp -o test.o -Wl,--gc-sections </code></pre> <p>(Note that -Os is an additional linker flag that tells GCC to optimize for size)</p> <p>I have also read somewhere that linking static libraries is different though. That GCC automatically omits unused symbols in this case. Perhaps another poster can confirm/disprove this.</p> <p>As for MSVC, as others have mentioned, function level linking accomplishes the same thing. I believe the compiler flag for this is (to sort things into sections):</p> <pre><code>/Gy </code></pre> <p>And then the linker flag (to discard unused sections):</p> <pre><code>/OPT:REF </code></pre> <p><strong>EDIT: After further research, I think that bit about GCC automatically doing this for static libraries is false.</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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