When should you turn code into a library?

I had a general question about C. I'm wondering when it makes since for a programmer to create a library? I am a novice at best so far in C and C# and am wondering when it really makes since to turn a group of functions into a library.

  1. Does this create additional overhead in Arduino code by calling a library?
  • I'll take LCD4Bit as an example. I have added additional functions in the library to do various
    display tasks. I won't always use these functions and I'm guessing calling that library adds
    the functions I don't use. Course that may show my lack of knowledge if I'm wrong.
  1. Is there a certain logical point where a few functions for a specific device should then be turned into
    a library?
  2. Does creating multiple program files make more sense than calling libraries?

I'm sure there are 50 other questions I could think of but these were definitely at the top of my list.

Thanks in advance all.

Having unused functions in a library is no more wasteful than having those same unused functions in a sketch. But yes, they will take up memory (and it's harder to just comment them out if they're hidden in another file).

I'd say you want to create a library if you're going to be using the same device / functions / files in multiple sketches. Having multiple files in the same sketch can be a good way to organize it, but once you start copying those files to other sketches, it's probably time to make a library.

Kewl, thank you for the response. That makes a lot of sense.

If a function in a library isn't called, it will not be included in the executable. This saves program memory.

If a function is in a sketch (or a standarrd .c file), it will be included even if it isn't called.

Saving space by leaving out unused functions is just one reason to use libraries.

-j

If a function in a library isn't called, it will not be included in the executable. This saves program memory.

If a function is in a sketch (or a standarrd .c file), it will be included even if it isn't called.

Saving space by leaving out unused functions is just one reason to use libraries.

-j

Unfortunately that is not the case here because although the Arduino environment calls the files in the library directory 'libraries' they are actually object files and the linker brings in all the code in the file, used or not. At least that's what it looks like when inspecting the runtime code dump in the elf file.

When I was referring to libraries, I meant libwhatever.a files - i.e., real libraries. I didn't realize Arduino libraries didn't work this way.

-j

Oh yeah that is a good point. I wonder what the difference is (as far as processing). I realize, I'm gonna have to cut some Arduino libraries down at this point as I'm taking half the memory for sketches at this point with my project and I have a ways to go.