Pre-compiled Library for esp8266

Been looking into creating some pre-compiled libraries for a project. Making the library was easy enough to find information on but I can't seem to find clear information on deploying for the esp8266 architecture. I have put the .a file in various folders in the src directory of the library and removed the .h and .cc files but nothing seems to detect the .a as the file.

I have tried esp8266, nodemcu, nodemcuv2, desp8266 but none of these seem to be the right name expected for the esp8266. Are any of these correct and i'm missing something else? I know for example the RP2040 is something like cortex-m0plus but I can't find the same thing defined for the esp boards.

Am I missing something or does esp8266 not support this?

I was able to create a precompiled library for the atmega328p architecture. The same process might work on other architectures:

Attempting to create small test precompiled sketch - #14 by johnwasser

My problem is I don't know what the src folder should be named for esp8266 and I can't find it documented anywhere. Once it's loaded in any of the ones I've mentioned it just fails because it's trying to resolve the .h file which doesn't exist anymore so that tells me it's not even trying to use the compiled library. Might be some more steps specifically around this architecture I'm missing but I haven't found anybody describing a success story with this specific board.

So you already set the necessary options in the library.properties file?

dot_a_linkage=true
precompiled=full

I would think [SKETCHBOOK]/libraries/[LIBRARY_NAME]/src/esp8266/[LIBRARY_NAME].a"

Yup, library.properties has those parameters set and the .a file exists in that location with the lib prefix. That was the first folder I tested but it doesn't seem to work. Tried to find if there was some other parameter that needs to be set to make it work with the esp architecture but couldn't find anything to confirm if the esp8266 was incorrect or if some other compile flag was needed, or platform.txt change, etc.

It is esp8266.

You can see an existing library that is precompiled for ESP8266 here:

(check src/esp8266/libalgobsec.a)

As mentioned in the somewhat outdated documentation for that library, the ESP8266 boards platform is not configured to work with precompiled libraries. This was fixed recently:

but there has not been a release since that time. So if you are using a release version of the platform (e.g., 3.0.2), you must manually apply that patch to its platform.txt file:

https://github.com/esp8266/Arduino/pull/8392/files

I can provide instructions for doing that if you would like. Fortunately, it is only a couple of lines and nothing complicated.

Followed along with the example repo and the other links and even though i've changed that specific line, it does not work. There seemed to be some necessity around setting the ldflags but I couldn't follow where the value came from or what I would need it to be or if I needed it at all.

Either way, trying to compile that repository, I get the following:

Compiling library "BSEC-Arduino-library-master"
Library BSEC-Arduino-library-master has been declared precompiled:
Using precompiled library in Arduino\libraries\BSEC-Arduino-library-master\src\esp8266
The platform does not support 'compiler.libraries.ldflags' for precompiled libraries.

I am also using the board manager 2.7.4 for this because of how much everything got changed in 3.+ for the web server code which breaks all my current projects until I can rewrite them.

Might just not support pre-compiled libraries until a later version? Any release notes on when this became available?

This is because you did not apply the patch from https://github.com/esp8266/Arduino/pull/8392/files

I don't think so. Forget about the ldflags for now. Just focus on getting a basic precompiled library working. I would start out with a simple blink.

That should be fine. But you still need to apply that patch.

Do you mean the ESP8266 boards platform support for precompiled libraries? If so, there are no release notes because it is not in any release. The fix was made 2021-12-03 and the latest release was on 2021-07-26. I'm sure it will be in the next release, but you can see the release cycle on that project is pretty slow, so I wouldn't hold my breath for a new release.

Looks like I missed adding

compiler.libraries.ldflags=

To the platform.txt and had only replaced the other line with the corrected pattern.

I had also upgraded to board 3.0.2 just to rule out any issue and then back down to 2.7.4 and both worked on the bsec repository.

Tried again on my code and same error so at least now I know the framework is right but something is missing from my project. I don't have any ldflags set in mine if that's an issue, just not sure what to set it to.

That is not an issue. I just made a simple precompiled library for ESP8266 without an ldflags field in its library.properties file and it worked just fine.

I think that custom ldflags can be useful in some advanced use cases, but it is not required.

The main issue I see right now is that it won't detect it's pre-compiled even though the precompiled parameter is set to true.

This is what the bsec shows up on the compiler:

Detecting libraries used...
Compiling library "BSEC-Arduino-library-master"
Library BSEC-Arduino-library-master has been declared precompiled:

But on mine it shows as:

Detecting libraries used...
Alternatives for NTPClient.h: []NTPClient:1:23: fatal error: NTPClient.h: No such file or directory

ResolveLibrary(NTPClient.h) #include <NTPClient.h>

-> candidates: []

Does your library contain a file named NTPClient.h? If so, where is it located in the library folder structure?

If not, you still need to provide a header file. This is required by the Arduino build system for library discovery. It will not recognize a library that does not contain at least one header file, even if the library is precompiled. And anyway, you should still provide declarations for the API of your library regardless of whether it is pre-compiled or not. Those declarations are not really a problem for either of the use cases of precompiled libraries (hiding source code or reducing sketch build time).

I was going from whatever forum post I was following and I'm pretty sure it said remove the .h and .cpp files to test out the pre-compiled libraries. I had expected to leave them there once I could test out it was using the pre-compiled versions first but I guess I have to leave the .h files regardless.

So seeing it using the pre-compiled with the .h file, I'm a bit closer although for some reason it's trying to resolve my .h file again but not in the src folder, just directly in the library folder but I think that's a problem not related to this.

That sounds like progress!

You can't remove the .h, but I do think it is a good idea to remove the .cpp files during testing so that you can verify that the binary is being used by the build system.

OK, well if you get stuck on that just let us know and we'll see if we can help.

The pre-compiled library is resolving the wrong location and i'm not sure why but this is what I found:

I see this in the output so I know it's looking at the right area initially:

Compiling libraries...
Compiling library "NTPClient"
Library NTPClient has been declared precompiled:
Using precompiled library in Arduino\libraries\NTPClient\src\esp8266

And then it gets to the end and, things go haywire:

Linking everything together...
xtensa-lx106-elf-gcc: error: AppData\Local\Temp\arduino_build_327635\libraries\NTPClient\NTPClient.a: No such file or directory

Wasn't sure why the library was now resolving here instead of the src\esp8266 after it resolved earlier. Realized that's the temp directory where it builds out the pre-compiled library. But why is it looking there?
Because I forgot to remove and/or set the dot_a_linkage property to false in my library.properties. So I did that and it compiles correctly with the pre-compiled library.

But then I compared that to the property to the bsec project and it does have that and set to true so there's is some way to have that set to true and use the pre-compiled libraries when available but whatever that is, I don't seem to have it set right.

I don't know the answer, but I would recommend investigating whether it is related to a specific combination of the following factors:

  • Is the precompiled field set to true or full
  • Does the library contain a .cpp file

The "BSEC Software Library" library has precompiled set to true and it does contain a .cpp file.

That checks out. I was thinking you could just leave it that way and it would just not compile and use the pre-compiled library only if no source was found.

I think I have a good understanding of all the requirements around this pre-compiled stuff now. Thanks for all the help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.