Thread: Which functions get included in the executable file of a program?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Which functions get included in the executable file of a program?

    (sorry if this is more a compiler question) I wonder what the compiler does with unused functions, does it neglect them while creating the executable? someone may say there's no point in defining functions that won't be used but (for example) writing a static library we have to do it and then any project linked against that library will usually use only "some" of the functions in there. I think non used functions are not included, because otherwise they will all add to the size of the executable... ?
    (I get confused because also if they are included that means(to me) that the linker is "dissecting" the object files in the library to only find and use the functions the program needs. Although this sounds like the more efficient decision, is that what's happening?)
    ---
    P.S.: And how about the same question about dynamic libraries? (thanks)
    Last edited by narniat; 12-03-2018 at 11:06 AM. Reason: adds a question

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,239
    Yes, only the functions that are used in your program are copied from the static library into your executable, although this may seem to include functions that you "didn't" use, since the ones that you did use make use them. Libraries are organized so that the functions can be looked up quickly.

    For dynamic linking, the functions are not included in the executable at all. Instead, they are "linked" (their addresses are determined) as the executable is loaded (load time). The dynamic library may already be in memory, used by another program, and you will be linked to the same copy. That's one of the advantages of a dynamic library, that the code is only stored once in memory even if a hundred programs are using it.

    EDIT: As laserlight points out, it depends on the compiler/linker/loader and basically parts of the system that are not specified in the C Standard, so your results may vary!
    Last edited by john.c; 12-03-2018 at 11:24 AM.
    Your logic makes me feel like a dick. - Bob Fossil

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    You probably could test this: stuff a great deal of code into a function, then have a minimal main function that doesn't call that function. Compile at a high optimisation level and examine the assembly output.

    If a library is meant to be dynamically linked, none of its code will be in your program anyway, so I think it's pretty clear there's also no unused functions to remove. If a library is meant to be statically linked, I'd guess it depends on how smart is the linker to ignore what is unused, and possibly also how the library is structured (e.g., perhaps the linker can ignore an entire object file in the library that was unreferenced, but not individual functions).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Thank you laserlight and john.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-16-2018, 11:03 PM
  2. Replies: 0
    Last Post: 05-16-2018, 11:03 PM
  3. Replies: 2
    Last Post: 01-25-2013, 01:22 PM
  4. Unable to open included file
    By jaisha in forum C Programming
    Replies: 12
    Last Post: 04-22-2010, 12:23 AM
  5. Replies: 9
    Last Post: 03-03-2006, 10:11 PM

Tags for this Thread