0

In my Arduino program, I have a set of different options for setup/loop functions that I'm planning to select using a #define macro. I have the following already:

namespace Part2 {
void setup() {
    // ...
}
void loop() {
    // ...
}
}  // namespace Part2

// ...

// Choose which tutorial video will be built
#define PREFIX Part2

void setup() { PREFIX::setup(); }

void loop() { PREFIX::loop(); }

However, I think this looks quite messy and I'm wondering if there's a way to define setup/loop as an alias to PREFIX::setup/PREFIX::loop instead of using this one-line function call. I've tried most of the methods suggested in these two SO posts (Post 1, Post 2), but both result in variants of the same compiler error:

src/main.cpp:27:15: error: 'void (* setup)()' redeclared as different kind of symbol

I think this is because the Arduino.h header file included in every program has already declared setup/loop and the syntaxes mentioned in the aforementioned SO posts deal with declaring/defining an entirely new function alias (as opposed to defining an already declared function as an alias to another). Is there an easy way to make this work?

ifconfig
  • 6,242
  • 7
  • 41
  • 65
  • when the function is declared in a different namespace there should be no clash, ie no error for redefinition. Please include a [mcve] – 463035818_is_not_an_ai Jun 29 '21 at 07:58
  • Why not put the common parts in a library and create a clean project for each tutorial that uses that library? – Ted Lyngmo Jun 29 '21 at 08:00
  • I don't quite understand. As far as I know, this is a minimal reproducible example for this question. I have multiple different "sub-programs" in separate namespaces that I want to be able to switch between using this `PREFIX` macro. As far as I can tell, I have refactored all of the common parts out. Could you please explain what you mean? – ifconfig Jun 29 '21 at 15:33
  • Would you like to see some of the example "sub-programs"? I can link to my repo on GitHub if you'd like... – ifconfig Jun 29 '21 at 15:36
  • minimal reproducible example means enough code so others can reproduce the same issue, but not more. When I copy the code you posted and try to compile it this is what happens: https://godbolt.org/z/ahhfvMfhe – 463035818_is_not_an_ai Jun 30 '21 at 12:12
  • Okay, I understand. Sorry, I thought that what I had already said describes the program structure I have. I've edited my OP to include some of the `Part2` namespace. – ifconfig Jun 30 '21 at 17:26
  • For the record, this is the Arduino project I'm referring to: https://github.com/neilbalch/ESP32-FreeRTOS-Tutorial – ifconfig Jun 30 '21 at 17:28

0 Answers0