Hello,
I am gonna apologise in advance for wildly missing the point and being extremely stupid….but I am confused as to the purpose of the lib folder. I think I am missing something obvious and perhaps overthinking. Yes, I have read the lib/README.md but it seems so simplistic as to be kinda useless. Thinking about the purpose of it confuses matters further. I am just gonna present my understanding and hopefully someone can tell me why I am wrong and illuminate me. Full disclosure I am not super comfortable with how Cpp manages distribution of code and dependency management and find the build systems a bit mysterious beyond the very basics
My question:
What does separating out my code there actually give me and why does this exist because it would seem it is not for the obvious reasons? …I would imagine that the answer should be enforced boundaries in my code and code reuse. This is the reason we separate code out afterall in any language, but this lib seems to do none of these/do none of these well…..
- Doesn’t Seem to Offer any Reuse advantages Over Subfolders in Src:
Normally when you separate code out into a library it is supposed to give you re-use. I mean if I were working in a rust repos I could turn a particularly large module into its own crate for reuse and then reference it in another project via something like:
[dependencies]
my_crate = { git = “``https://github.com/username/my_crate.git”``, package = “subcrate_name” }
but if I am understanding correctly, there is no way built in to reuse stuff from the lib folder in another project; you have to do it manually in some form. We are reduced to copy and paste/git submodules which is kinda gross…..If we are doing that, then we might as well just separate out our code under src/ into folders for logical organisation and then use gitsubmodules and copy paste with this whcih seems equally crude.
So it seems that lib offers no advantages for code reuse at all over subfolders under src. I guess that it does offer the granular ability for libraries to carry their dependencies with them for reuse which is useful, but without a way to distribute them and without a way to enforce no coupling with other libraries or pio managed libraries this doesn’t seem very good…(explained in more detail in next point)
- lib contained libraries do not have any enforced library boundaries
I thought that perhaps a weakness of having everything in organised subfolders of src is there is no enforced code boundaries (everything can use everything ) leading to everything getting messy and interdependent.
So I thought that perhaps lib was intended so that you had to declare explicitly all dependencies of a library so that - for example - when working in projectA that contains the lib/libA you can’t accidentally make it dependent on lib/libB without explicitly declaring it in the library.json as a dependency and thus break projectB which was using libA (perhaps as a git submodule) without realising it. In other words, it prevents accidental coupling.
This would be a nice use and explanation for lib, but it seems that things will work even if you don’t declare the dependencies in the json, so it isn’t this. So for enforcing boundaries it is no better than subfolders in src when it comes to boundaries between othe rproject local code and/or external dependencies which is obviously disappointing.
- Perhaps it is nothing to do with code reuse and is just for build system clarity so that code native to the repos is not compiled when not needed?
from the README.md:
The PlatformIO Library Dependency Finder will find automatically dependentlibraries by scanning project source files.More information about PlatformIO Library Dependency Finder
So the dependency finder can find whcih libraries your code actually depends on (perhaps you have a library that is only used in your code in certain compilation modes)…. but then if you aren’t using anything from that library in a given mode then it will get weeded out by the linker anyway if I am understanding at the point where we stitch the .o files together….so this hardly does anything…?
it also seems that the build process in Scons that pio uses is clever enough not to build every subfolder under src each time when there are only changes to a few files, so it is not as if separating out this code into libraries somehow enables better dependency tracking between files for incremental compilation….
Conclusion:
Probably missing something extremely obvious, but the only thing that I can think of is that it allows you to add dependencies to your project in a granular way and tie dependencies to the code that actually uses it so that when that modular code is distributed it brings with it all of its dependencies. This is great and all! However, without enforcing include paths limited ONLY to explicit dependencies this is kinda half baked….and if you git submodule a lib from inside a project then as far as I know pio isn’t clever enough to download any local dependencies it had in the project it came from…rendering this kinda useless again. So I am guessing it isn’t for this reason. Is it something simpler? Is it build process related?
thanks for any guidance