Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4048264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:47:42+00:00 2026-05-20T13:47:42+00:00

I asked myself the following question, when I was discussing this topic . Are

  • 0

I asked myself the following question, when I was discussing this topic .

Are there cases when some unused code from translation units will link to final executable code (in release mode of course) for popular compilers like GCC and VC++?

For example suppose we have 2 compilation units:

//A.hpp
//Here are declarations of some classes, functions, extern variables etc.

And source file

//A.cpp
//defination of A.hpp declarations

And finally main

//main.cpp
//including A.hpp library
#include "A.hpp"
//here we will use some stuff from A.hpp library, but not everything

My question is. What if in main.cpp not all the stuff from A.hpp is used? Will the linker remove all unused code, or there are some cases, when some unused code can link with executable file?

Edit: I’m interested in G++ and VC++ linkers.

Edit: Of course I mean in release mode.

Edit: I’m starting bounty for this question to get good and full answer. I’m expecting answer, which will explain in which cases g++ and VC++ linkers are linking junk and what kind of code they are able to remove from executable file(unneeded functions, unneeded global variables, unneeded class definitions, etc…) and why aren’t they able to remove some kind of unneeded stuff.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:47:43+00:00Added an answer on May 20, 2026 at 1:47 pm

    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.

    For GCC, this is accomplished in two stages:

    1. 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:

      -fdata-sections -ffunction-sections

    2. Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):

      -Wl,–gc-sections

    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++):

    gcc -Os -fdata-sections -ffunction-sections test.cpp -o test.o -Wl,--gc-sections
    

    (Note that -Os is an additional linker flag that tells GCC to optimize for size)

    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.

    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):

    /Gy
    

    And then the linker flag (to discard unused sections):

    /OPT:REF
    

    EDIT: After further research, I think that bit about GCC automatically doing this for static libraries is false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(I asked this question in another way , and got some interesting responses but
this is more a theoretical question i asked myself. I remembered that BinarySearch of
I asked this question earlier but I didn't express myself correctly. If I have
I know this question has been asked many times and I've researched it myself
I've always asked myself this question, particularly when i see how JPA 2.0 metamodel
While coding, i just asked myself this question : Is this faster : if(false)
I've asked myself this question a number of times when creating classes, particularly those
This is a question I have asked myself many times in the past as
Working with new pieces of code, I asked myself a question : Are multiple
I'm quoting myself on a previous question I asked to further explain: I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.