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

  • SEARCH
  • Home
  • 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 782715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:25:48+00:00 2026-05-14T20:25:48+00:00

When I include some function from a header file in a C++ program, does

  • 0

When I include some function from a header file in a C++ program, does the entire header file code get copied to the final executable or only the machine code for the specific function is generated. For example, if I call std::sort from the <algorithm> header in C++, is the machine code generated only for the sort() function or for the entire <algorithm> header file.

I think that a similar question exists somewhere on Stack Overflow, but I have tried my best to find it (I glanced over it once, but lost the link). If you can point me to that, it would be wonderful.

  • 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-14T20:25:48+00:00Added an answer on May 14, 2026 at 8:25 pm

    You’re mixing two distinct issues here:

    1. Header files, handled by the preprocessor
    2. Selective linking of code by the C++ linker

    Header files

    These are simply copied verbatim by the preprocessor into the place that includes them. All the code of algorithm is copied into the .cpp file when you #include <algorithm>.

    Selective linking

    Most modern linkers won’t link in functions that aren’t getting called in your application. I.e. write a function foo and never call it – its code won’t get into the executable. So if you #include <algorithm> and only use sort here’s what happens:

    • The preprocessor shoves the whole algorithm file into your source file
    • You call only sort
    • The linked analyzes this and only adds the source of sort (and functions it calls, if any) to the executable. The other algorithms’ code isn’t getting added

    That said, C++ templates complicate the matter a bit further. It’s a complex issue to explain here, but in a nutshell – templates get expanded by the compiler for all the types that you’re actually using. So if have a vector of int and a vector of string, the compiler will generate two copies of the whole code for the vector class in your code. Since you are using it (otherwise the compiler wouldn’t generate it), the linker also places it into the executable.

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

Sidebar

Related Questions

I have some C++ code #include <cstdio> #include <boost/bind.hpp> #include <boost/function.hpp> class A {
I'm writing some quick code to try and extract data from an mp3 file
From http://www.learncpp.com/cpp-tutorial/19-header-files/ How does the linker know that it needs to include the standard
I want to include some dynamic part in the filename of the msi file
I'm packaging an ejb and I need to include some .classes from a dependency
Possible Duplicate: Why can templates only be implemented in the header file? Undefined reference
Working on a qt project using msvc2008 compiler. I copied some functions from an
I am trying to load some data from a text file into a vector
I've got some code originating from a DSL that needs to be translated for
I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward

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.