Connecting Tech Pros Worldwide Forums | Help | Site Map

Does a compiled C application have the complete standard library?

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,953
#1: Oct 2 '09
When you compile and link a C application, is the whole standard library plopped in their, too, or just those files you specify with your #includes?

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Oct 2 '09

re: Does a compiled C application have the complete standard library?


Quote:

Originally Posted by Markus View Post

When you compile and link a C application, is the whole standard library plopped in their, too, or just those files you specify with your #includes?

Include files have nothing to do with what and what not is linked against your file. It all depends on your linker: extremely stupid linkers could link the entire library (which is just a bunch of compiled object files) against your file. A bit more clever linkers only link those objects that are referenced from your file and nowadays linkers use dynamic linking.

kind regards,

Jos (ex-moderator)
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,953
#3: Oct 2 '09

re: Does a compiled C application have the complete standard library?


So it depends on the linker? Right.

What do you mean by 'dynamic linking'?

Thanks, Jos.

Mark.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Oct 2 '09

re: Does a compiled C application have the complete standard library?


Quote:

Originally Posted by Markus View Post

So it depends on the linker? Right.
What do you mean by 'dynamic linking'?

Linking against .dll files instead of .lib files (on a Microsoft Windows platform) or linking against .so files instead of .a files (on a Unix platform). Dynamic linking is linking during runtime; static linking happens before the process can run.

kind regards,

Jos (ex-moderator)
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,210
#5: Oct 2 '09

re: Does a compiled C application have the complete standard library?


On Windows it is mildly more complex than that because you can have linker time dynamic linking, that is where you link your program with a .lib, the .lib contains code to call the .dll, the program will not start if it can not find the .dll.

Or you can have run time dynamic linking, that is where you local the library (using the API function LoadLibrary) at run time and use the function GetProcAddress to get the address of procedures with known names in the library which you can then call. The program can start without the library being present.

The fact that intelligent (and most of the well known ones are intelligent) linkers only link in the required code is a reason why (especially in a C library) it is a good idea to keep your source files as small as logically sensible. An object file normally being the smallest unit a linker and choose to link or not link.

It is also the reason that on many embedded platforms you often get 2 versions of printf, with and without floating point support. Since the floating point library for embedded platforms is often large and floating point arithmetic is often not used not having the library in makes a significant difference to program size. But if the printf without floating point support did not exist then any use of printf would automatically pull in the floating point library.
Reply