PlatformIO Community

Is there a way to have multiple main files for different builds?

I’m writing 2 programs that will run on two separate Arduino’s, but they share a lot of common code. Is there a way to have different main files within one project?

The src files would look like:

  • main1.cpp
  • main2.cpp
  • common.cpp

Just doing it like that of course results in multiple definition errors for init and loop.

Check the src_filter option, different sections can have different filters:

http://docs.platformio.org/en/latest/projectconf/section_env_build.html

Or use -Dname in build_flags to define two names and then #ifdef in your code.

1 Like

I tried to do something like this in platformio.ini:

[env:master]
platform = atmelavr
board = uno
framework = arduino
src_filter =
    ${common_env_data.build_flags}
    -<src/slave.cpp>

[env:slave]
platform = atmelavr
board = uno
framework = arduino
src_filter =
    ${common_env_data.build_flags}
    -<src/slave.cpp>

But that does’t work as common_env_data isn’t defined, how should I get the default build_flags? And am I going in the right direction?

if you use ${common_env_data.build_flags}, then you have to have a section like this:

[common_env_data]
build_flags = ...

But this might be a simpler way:

[env:master]
build_flags = -DMASTER
platform = atmelavr
...

[env:slave]
build_flags = -DSLAVE
platform = atmelavr
...

Then use #ifdef MASTER and #ifdef SLAVE around your code.

3 Likes