Skip to main content
Upd in response to @Juraj's question
Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53

One way to do it is to use preprocessor conditionals. Name your project file and unit-test files with .cpp extensions and put them in a subfolder (called "sources" in my example). Then your .ino file would only be used to instruct the preprocessor which of the several "main" file to start compiling with:

myProject.ino:

/*
 * myProject.ino
 * define which "main" CPP file to compile:
 */

#define PRODUCTION
// #define UNITTEST1
// #define UNITTEST2
// #define UNITTEST3

// The compilation starts here with the requested "main" file:
#if defined PRODUCTION
#include "sources/myProject.cpp"

#elif defined UNITTEST1
#include "sources/unitTest1.cpp"

#elif defined UNITTEST2
#include "sources/unitTest2.cpp"

#elif defined UNITTEST3
#include "sources/unitTest3.cpp"

#else
// If nothing defined, compile for production
#include "sources/myProject.cpp"
#endif

/* END */

Only the .ino file would need to be edited to compile one of the unit tests, and only one project file would be needed.

Update: "did you try it? "

Yes, and it does work. The only caveat I'd add is: don't name your subfolder "src" - that seems to be a magic-name to the IDE, and you'll get complaints of compilation errors (multiple definitions) if you use it. And further, if you try it, you can't get back simply by changing the folder's name (on disk and in the code); you'll have to restart the IDE as well.
I tested it with v1.8.19 of the IDE.

One way to do it is to use preprocessor conditionals. Name your project file and unit-test files with .cpp extensions and put them in a subfolder (called "sources" in my example). Then your .ino file would only be used to instruct the preprocessor which of the several "main" file to start compiling with:

myProject.ino:

/*
 * myProject.ino
 * define which "main" CPP file to compile:
 */

#define PRODUCTION
// #define UNITTEST1
// #define UNITTEST2
// #define UNITTEST3

// The compilation starts here with the requested "main" file:
#if defined PRODUCTION
#include "sources/myProject.cpp"

#elif defined UNITTEST1
#include "sources/unitTest1.cpp"

#elif defined UNITTEST2
#include "sources/unitTest2.cpp"

#elif defined UNITTEST3
#include "sources/unitTest3.cpp"

#else
// If nothing defined, compile for production
#include "sources/myProject.cpp"
#endif

/* END */

Only the .ino file would need to be edited to compile one of the unit tests, and only one project file would be needed.

One way to do it is to use preprocessor conditionals. Name your project file and unit-test files with .cpp extensions and put them in a subfolder (called "sources" in my example). Then your .ino file would only be used to instruct the preprocessor which of the several "main" file to start compiling with:

myProject.ino:

/*
 * myProject.ino
 * define which "main" CPP file to compile:
 */

#define PRODUCTION
// #define UNITTEST1
// #define UNITTEST2
// #define UNITTEST3

// The compilation starts here with the requested "main" file:
#if defined PRODUCTION
#include "sources/myProject.cpp"

#elif defined UNITTEST1
#include "sources/unitTest1.cpp"

#elif defined UNITTEST2
#include "sources/unitTest2.cpp"

#elif defined UNITTEST3
#include "sources/unitTest3.cpp"

#else
// If nothing defined, compile for production
#include "sources/myProject.cpp"
#endif

/* END */

Only the .ino file would need to be edited to compile one of the unit tests, and only one project file would be needed.

Update: "did you try it? "

Yes, and it does work. The only caveat I'd add is: don't name your subfolder "src" - that seems to be a magic-name to the IDE, and you'll get complaints of compilation errors (multiple definitions) if you use it. And further, if you try it, you can't get back simply by changing the folder's name (on disk and in the code); you'll have to restart the IDE as well.
I tested it with v1.8.19 of the IDE.

Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53

One way to do it is to use preprocessor conditionals. Name your project file and unit-test files with .cpp extensions and put them in a subfolder (called "sources" in my example). Then your .ino file would only be used to instruct the preprocessor which of the several "main" file to start compiling with:

myProject.ino:

/*
 * myProject.ino
 * define which "main" CPP file to compile:
 */

#define PRODUCTION
// #define UNITTEST1
// #define UNITTEST2
// #define UNITTEST3

// The compilation starts here with the requested "main" file:
#if defined PRODUCTION
#include "sources/myProject.cpp"

#elif defined UNITTEST1
#include "sources/unitTest1.cpp"

#elif defined UNITTEST2
#include "sources/unitTest2.cpp"

#elif defined UNITTEST3
#include "sources/unitTest3.cpp"

#else
// If nothing defined, compile for production
#include "sources/myProject.cpp"
#endif

/* END */

Only the .ino file would need to be edited to compile one of the unit tests, and only one project file would be needed.