Thread: Linking question

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    Linking question

    Okay, I have a small gap in my knowledge base. When you link a library into your program does the whole library, every function whether you use it or not, get added to your program, or is the linker smart enough so that if you're only using printf that it doesn't add anything but what you need for printf?

    Okay, I'm not worried about hello world. Actually it's PDCurses. I just discovered that to get curses to run on my instillation of Code::Blocks that I, in my igorance, made the compiler always link the library for curses. So does this mean that every executable I produce has the entire curses library attached to it, even if I don't #include <curses.h>?

    And what about hello world? If all i use is printf in a program are scanf, puts, and the rest hidden in there, dormant and unused?
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It depends on the linker, but modern linkers will work like this:

    If you're using a shared library, nothing actually gets linked into your executable. It finds the library at runtime and loads it then.

    If you're using a static library, only those symbols which you use (and which those symbols use, etc: As you noted, using printf() will likely bring in lots of other related stuff). The whole library will not be attached to your program.

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    See, that's what I figured, but you can never be sure about these things. I wanted to take PDCurses library out of my linker and see if hello world has any decrease in size. Maybe I will this weekend.

    I did, howver, notice, that when I re-write hello world to C++ there was a dramatic increase in the executable. Which is what got me wondering.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    g++ and the related library for C++ is pretty bad when it comes to keeping itself down to size. This is mainly due to the iostreams templates dragging in the everything, kitchen sink and all - for example, it drags in printf, which shouldn't be needed for C++ normally.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can still use a shared library. That way, no stuff will get drawn into your app. Just install the runtime once and poof, everything magically works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Is there a shared library for PDCurses?
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fmod question
    By spadez in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 05:26 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Beginner Question - Linking a data file
    By noiprocs2 in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2008, 08:58 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM