Native Unit Test on M1 Mac

I want to unit test natively on an M1 Mac. Therefore I have the following simple code located at /test/test_main.cpp:

#include <unity.h>

void simple_test(void) {
  TEST_ASSERT_EQUAL(1, 1);
}

void setup() {
  UNITY_BEGIN();  // IMPORTANT LINE!
  RUN_TEST(simple_test);
}

void loop() {
  UNITY_END();  // stop unit testing
}

Now I get the following error when trying to execute the test natively:

Building...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
g++ -o .pio/build/latest_stable/test/test_main.o -c -DPLATFORMIO=50203 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Isrc -I.pio/build/latest_stable/UnityTestLib -I/Users/kaikuklok/.platformio/packages/tool-unity test/test_main.cpp
g++ -o .pio/build/latest_stable/program .pio/build/latest_stable/test/test_main.o .pio/build/latest_stable/test/tmp_pio_test_transport.o -L.pio/build/latest_stable .pio/build/latest_stable/libUnityTestLib.a
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pio/build/latest_stable/program] Error 1

My settings platformio.ini looks like the following for native:

; Latest stable version
[env:latest_stable]
platform = native
test_ignore = test_embedded

Has somebody an idea how I can get this to work?

1 Like

The error message is correct, you have not defined main() – just setup() and loop(), which is Arduino specific and thus don’t get called in the test environment for a desktop.

Per official example you should do it in the style

If you want to use the same test file for both embedded + desktop tests, see here.

3 Likes

Thanks for clarifying that. It works now as expected.
Sometimes you just don’t get it …

I will strongly disagree. The error messages does not suggest anything, unless you already know what’s going on, because you’ve stumbled upon that before.


pio run -e test
Processing test (platform: native)
--------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 4 compatible libraries
Scanning dependencies...
Dependency Graph
|-- ArduinoFake @ 0.3.1
Building in release mode
Linking .pio/build/test/program
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pio/build/test/program] Error 1

That’s the error that I didn’t understand, until I came upon this thread from googling platformio implicit entry/start for main executable.

Undefined symbols suggests to me that I have used some kind of symbols (like $ or <).

for architecture x86_64 misleads me into thinking about CPU architectures, does having x86_64 really make a difference in this situation?

"_main" – I grep for this and find nothing.

referenced from: implicit entry/start for main executable – how can I go there and look for the problem? which file, which line is it?


The message could be like:

Error: you must define int main(int argc, char **argv) function, required to execute the program.

Or ike this [let’s reuse the wording that was poorly chosen in a better context]:

Error: Required implicit entry/start for main executable: missing int main(int argc, char **argv) function.

1 Like