Beginner question: compiling a library for Arduino in PlatformIO?

Hi all. This is probably something really simple that I’m missing. I’ve written a library in C++ for the Teensy 3.6, but I wrote it in PlatformIO. So (maybe obviously), when I try to compile the library, it complains that there is no setup() or main() function. I imagine that I should compile using a standard C++ compiler - is there an easy way to convert this project from a PlatformIO project to a standard C++ one, and will it automatically find my Arduino compatible libraries (e.g., #include <SPI.h>)? Sorry for the very novice question!

If you wrote your library correctly and it compiles in PIO there should be no problem in the Arduino IDE. I think the problem is rather specific to your code. Are you sure that you not only compile the library code but also the example code that has setup() and main()? Or do you have actually want to compile a .a static ibrary?

sorry, I meant that it doesn’t compile in PIO. It throws the “missing setup() and loop()” error when I try to compile the library separately. As in, I don’t think it knows it’s a library, and not code specific to something to be uploaded to a Teensy.

So what PIO is supposed to do is compile just compile the code to a .a library? What’s the desired output?

1 Like

sorry for the confusion. I’d like to just compile both a header .h and a .cpp file. Then I’d include the header in my Teensy code to be uploaded. I’m not familiar with the .a library, is it preferred over a header and cpp file for PIO?

Usually you just compile your sketch which uses the library and has the setup() and loop() functions. You can compile the library in isolation (.a) but doing so is rather complicated and unusual. Your library code will be compiled when the main project is compiled then. If the library has any compile errors it will show up during the project compile.

interesting, so is the typical way to debug library bugs to include it in a sketch and compile the sketch? Basically, I just want to debug the library and make sure everything seems good before I stick it in a sketch, but if the workflow is typically different than that, it would be great to know!

How exactly? Just by compiling the code itself and seeing if there are compile errors? Or by unit testing? Redirecting...

Also Recommended project setup for library development might help.

Generally (and I’m betting this will be the case with your code) a released library has been debugged fairly well and what you need to worry about is your own work. However, if all you want to do is make sure a library will compile correctly you can do something like this:


#include <NameOfLibrary.h>

setup() {}

loop() {}

This is just a blank program with the expected/required setup() and loop() and will include in the library you want to test compile.

Sorry, I meant I’ve written a library (.cpp + .h) for a chip, and I want to test the library for compilation errors, and fix those. It doesn’t really matter what method you guys recommend, but I think it’s best if I learn the “standard” way of doing that. If making an empty file with an empty setup() and loop() is the way to do it, then that’s what I’ll do

If you want to test the library… your code actually needs to use it… you can’t just compile it in isolation. So if you want to build it against the arduino framework to test it… you need to have a minimum in your main.cpp

#include <Arduino.h>

void setup() {}

void loop() {}

in order to satisfy the Arduino framework requirements. Then, of course, you need to #include your library. That should be enough to get the code pulled in and compiled. Or you might need to create an object or call a function from that library in order to make sure the compiler doesn’t go hey, you didn’t actually use that library… not going to bother looking at that

The way I test the Arduino libraries I work on is to have examples sketches that utilise as much of the library code as possible, and then have CI jobs run the Arduino Builder over the example sketches every time I make a commit to make sure I don’t break something unknowingly. But, yeah, you need to include the library in some code and actually call stuff from from it for it to get compiled and used. :wink: