Reputation: 175
It seems we are free to use Windows API simply by including header files needed. However, i couldn't make myself understand how this is possible given that header files do not have function definitions but declarations, which is supposed to be considered as an error in that it lacks implementation details. In what way does a compiler locate an implementation detail and map it into memory?
Upvotes: 5
Views: 2779
Reputation: 134125
The Windows API is implemented in DLLs that are installed when you install Windows. Those DLLs are in the System directory and have names like User32.dll, Kernel32.dll, etc.
As you correctly noted, the Windows API header files supplied with your compiler contain only the declarations. When your C program calls one of those functions (or any other function that's identified in a header file but not actually compiled with your project), the compiler places a record in the generated object file that says, in effect, "I need to call a function called GetWindowRect
(or whatever the name of the function you called)."
The linker is what actually resolves the names. If you look at your linker options, you'll see that it's linking some libraries like User32.lib, Kernel32.lib, etc. Those libraries contain compiled functions that are little more than stubs--code that causes the corresponding DLL to be loaded, and then calls the function in the DLL.
It's a little bit more involved than that, but that's the general idea. In summary:
Upvotes: 6
Reputation: 968
It works the same as any other library, for example C stdlib
- your program only needs to 'know' the prototypes of functions (and thus the underlying symbol names). After compilation comes linking - that's when the symbols (function definitions) that are missing get 'linked' with a correct library.
Upvotes: 2