I have a few questions about libraries and header files. Since libraries are binary files and header file are source/text files why do they not just compile the header files along with the rest of the library files? It doesn't make sense to me to have libraries that are binary file and thus not editable but package them with source/text files that can be easily edited including edits that don't match the libraries they support.

Also. Since C the programming language is so small without the standard library how was the standard library created? How are any libraries created? I read that although a lot of GUI based programs are created in C, C is actually only capable of creating graphics with the aid of libraries, how are these libraries created?

Recommended Answers

All 2 Replies

why not just compile the header files along with the rest of the library files?

When you compile your code, the compiler has to know about library functions you are using. It has to know what they're called, and what parameters they accept, and what kind of parameter they return. This information typically comes from the header file.

As your code is compiled, the compiler effectively puts a note in the compiled code saying "Dear Mister Linker, please go looking for a function with this name, in this namespace, that accepts these parameters, and returns one of these." If the compiler does not know this information, there is no way to tell the linker which functions in the library you want to use.

We could create libraries that could be quizzed by a compiler and return the information about what functions they contain, but that's not how it works in C. I expect that in some programming languages that is how is works.

How are any libraries created?

Write source code, compile it into objects, jam objects together with some housekeeping information so linkers can use it. That's a library. Libraries are just functions and classes. Open up the source code of the standard libraries and you'll see they're written in C (and some assembler at times, I expect). Here's one: https://github.com/lattera/glibc

I read that although a lot of GUI based programs are created in C, C is actually only capable of creating graphics with the aid of libraries, how are these libraries created?

Graphics are controlled (on a modern OS) by the operating system. Whoever wrote the operating system included functions to tell the operating system what to display on the screen. When you do GUI work in C, you either use those functions directly, or you get another library which presents you with a nicer set of functions to call, and it talks to the OS for you.

These libraries are created by, as you can guess, writign source code, compiling it, jamming it together with some housekeeping information (thank you, linker).

We could create libraries that could be quizzed by a compiler and return the information about what functions they contain, but that's not how it works in C. I expect that in some programming languages that is how is works.

For example, C#/.NET

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.