From http://www.learncpp.com/cpp-tutorial/19-header-files/
How does the linker know that it needs to include the standard runtime library and for example the definitions of the functions declared in iostream are not present elsewhere?
Is there some mapping that facilitates the linking process?
To rephrase - If I include some file with only function declarations, how does any linker figure out where the function definitions are?
If you use g++
, it will always link the standard c++ library (-lstdc++
).
To bypass it, you can use gcc
and link a different library.
Edit: the linker doesn't figure out anything. The standard library is automatically linked, so there's nothing to figure out for the standard functions. If you just declare something that is not in STL and try to use it without the actual definition, the linker will fail. You have to manually link the library/.o.
This is just one of the default settings for your linker. Generally, you can tell it not to link the standard libraries if you don't wish to use them.
And you always have to tell the linker explicitly to link in any additional libraries that your application makes use of. It isn't going to "figure out" where the function definitions based solely on included declarations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With