How does a library work?

Hi
Working in the field of industrial automation I'm used to program industrial controllers like PLC's, and when playing around with my Arduino one thing really bothers me. Libraries. Feel free to laugh but I don't quite understand how they work.
When I write a program for a PLC I often use function blocks which basically are pre-programmed sections especially made for controlling a temperature, starting/stopping a motor or similar. Correct me if I'm wrong but I would compare these function blocks to the libraries of an Arduino and say that they've got the same purpose in a program.
Someone has written the program inside the function block so other programmers may save some time as they can copy/paste these program parts all over their own program when needed. I guess you can say the same about libraries,right?
I've already made programs using libraries found on different websites, but when adding them into the program (or should I say "sketch") I simply write the name of the library. So the library itself is laying around on the hard drive of my PC but somehow the Arduino knows what to do with the name of the library mentioned in my sketch...?
Is the name written in the sketch working as a link, so the Arduino will download the program behind the name of the library from my hard drive when compiling?
Is it possible to open, read and perhaps modify a library???

Mathniel:
Is the name written in the sketch working as a link, so the Arduino will download the program behind the name of the library from my hard drive when compiling?
Is it possible to open, read and perhaps modify a library???

Yes, almost. It actually appends all the library code to your sketch before compiling. It's basically a simple file inclusion.

You can navigate to a library folder and open the .h and .cpp files with a text editor to read them.

You can modify a library as easily as writing a sketch.

But.....be warned (from painful personal experience) - If you modify a library and say a year or two later decied to update your code it is likely that you will have updated the libraries in that time and all your changes were lost!

skywatch:
You can modify a library as easily as writing a sketch.

But.....be warned (from painful personal experience) - If you modify a library and say a year or two later decied to update your code it is likely that you will have updated the libraries in that time and all your changes were lost!

If they are OO you can easily modify them by creating classes of your own making that inherit from library classes. This leaves the original library untouched, but allows you to tailor them to your application.

I never allow the IDE to update libraries for me, and I resent the nag pop up that interrupts my work flow.

Thanks for you're answers.
So the IDE is not showing the complete code that is compiled and transferred? If I include a library, potentially, many Kb of code that I can't acces from my IDE could be included even before I have started writing the program itself?

Mathniel:
Thanks for you're answers.
So the IDE is not showing the complete code that is compiled and transferred? If I include a library, potentially, many Kb of code that I can't acces from my IDE could be included even before I have started writing the program itself?

The IDE doesn't show the complete code. Every sketch has included what would otherwise need to be added such as iostream, avr, or whatever else the most basic libs are for functionality.
Then if you explicitly include library (links to them in the header), then yes, the size of the library takes up sketch code space. Wouldn't work any other way.

Mathniel:
Thanks for you're answers.
So the IDE is not showing the complete code that is compiled and transferred? If I include a library, potentially, many Kb of code that I can't acces from my IDE could be included even before I have started writing the program itself?

You can access it. It's not hidden. It's right there in the libraries folder.

INTP:
The IDE doesn't show the complete code. Every sketch has included what would otherwise need to be added such as iostream, avr, or whatever else the most basic libs are for functionality.
Then if you explicitly include library (links to them in the header), then yes, the size of the library takes up sketch code space. Wouldn't work any other way.

It depends what you mean by "code space", source code or compiled code. It is the compiled code size that matters the most with Arduino, due to the limited memory available. The compiler has optimization. One of the optimizations is to check for functions that are defined but never called. Thus you can include a huge library and it won't add a single extra byte to your compiled sketch unless you actually call functions from it. If you call a certain function (i.e. use it), then that function and any other code that it calls will add to the size of your sketch. Otherwise not.

aarg:
Yes, almost. It actually appends all the library code to your sketch before compiling. It's basically a simple file inclusion.

No, that is not even close to correct. The sketch, and any referenced libraries, are all compiled separately - the library source code is never in any way appended to the sketch source code. The executable (HEX or BIN file) is created by linking the sketch object code to the object codes for all included libraries,

Regards,
Ray L.

So the library itself is laying around on the hard drive of my PC but somehow the Arduino knows what to do with the name of the library mentioned in my sketch…?

Yes. Exactly that.
Arduino libraries, being designed to run on several slightly-different but source-code-compatible boards, are distributed in source code form, and are stored in a couple of “standard” places on the hard drive (three that I can think of: standard libraries within the arduino core “hardware” directory, per-platform libraries in the directories installed by the board manager, and user-installed libraries that live in a sub-directory of the user’s sketchbook folder.)

When you put a “#include <ds1302.h>” in your sketch and hit the verify or upload buttons, the Arduino “pre-processor” sees that line, searches the standard library location, finds the source code, and adds the commands needed to compile that library to the list of commands that it’s going to use to build the sketch. If you check the “verbose” button next to “compile” in the preferences panel, you can watch most of this go by (well, it will end up in the build windows, and then you can cut&paste it to somewhere where you can look at it…)

RayLivingston:
No, that is not even close to correct. The sketch, and any referenced libraries, are all compiled separately - the library source code is never in any way appended to the sketch source code. The executable (HEX or BIN file) is created by linking the sketch object code to the object codes for all included libraries,

Regards,
Ray L.

Right. I don't think about the linker much, as it hides in the background. But the behaviour is "as if" the file were appended. I think that applies to hierarchical includes as well, perhaps not always with the Arduino IDE implementation of C.

I was trying to convey a concept that the OP could use to understand the behaviour.

Thanks for all your answers!