Separate declarations and source #12
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a
.cpp
file to allow the library to be included in multiple places. This is a revival of previous PR attempts: #2, #7. I believe previous PRs have already addressed the need for such a change.I've ensured the program compiles with
make test
. Surprisingly, the headers and implementation had different signatures. I've also fixed some issues with a test case violating the assumption that 62 is+
and 63 is/
.To address some of your (valid) concerns from previous PRs for posterity.
True. The compiler needs more work creating a separate object file and linking.
Traditionally, people have kept large implementations contained to headers by using
inline
/static
. Problem with this is that this will either 1) increase code size by inlining the function everywhere or 2) cause multiple separate functions (sincestatic
keeps symbols local within modules).Nowadays, header-only files are mostly used with classes (which inlines everything by default) and templates.
To save space, we usually use a source file instead of keeping everything to a header using
inline
/static
. It should compile faster as well, especially when using a build system such as make/cmake, which won't recompile unless changed. Most other Arduino projects I see do this as well: DHT, Adafruit stuff.Multiple inclusion within the same source isn't the issue here. It's with multiple inclusion in different sources. For example:
Error:
Multiple functions are defined in different sources, and the linker no likey because it doesn't know which symbol to link.
Imagine our pain and struggle when working on a project and being forced to include a file only in a single place. 😩