Separating `native` and common code bases in the project


I have a project where some part of it is independent from actual platform I want to use. Actually this part can be used by different platforms, let’s say it is like a calculator logic. I want to unit test this part separately and also have tests for each platform.

How should I organize project structure?

In one tutorial I saw that it can be done if I put this code under library folder. If this is the case, where should I place tests and how can I run them.

I was thinking about something like this actually:

and platformio.ini specifies which folder can be used by which sub-project.

A src_filter per-environment can help you by excluding / including certain folders to be included in the build. You can also create libraries which have a HAL-like structure for each target platform, see extraScript example for a library.json.

1 Like

Cool, where does main.cpp for Arduino goes? Will PlatformIO figure out it? and where should I put main.cpp for native?

It does not matter where this file is as long as it’s compiled the linker will find the needed setup() and loop() functions. Preferably somewhere src/ folder of course as opposed to the lib/ folder (though it’s also possible but weird looking). You just need to make sure it selects the right files for the build.