Building unit tests failed

I’m trying to run unit tests in an Mbed project. I’ve followed the instructions at Unit testing - Debugging and testing | Mbed OS 6 Documentation. I’m on MacOS 11.1.

I can create a new program using the mbed-os-empty template, but when I run mbed test --unittests from the root of the program directory, I get a “Building unit tests failed.” error.

The error seems to originate in the mbed-os/UNITTESTS/target_h/platform/mbed_retarget.h file.

If I navigate to that file in Mbed Studio, line 22 has the sys/types.h text underlined with a red squiggle (hovering over it says “file not found”).

That seems to cause a cascade of errors, including curiously enough some files buried in the MacOSX11.1.sdk directory (why it’s even looking for stuff in the Mac system SDK is beyond me).

The unit tests build for your local machine, which is why it’s referencing the Mac SDK. If you want to run tests on your target board, you might be looking for the Greentea test system instead.

Can you post the specific compiler error message?

Can you try?

# first install gmake and gcc
$  brew install gmake gcc

# then from the unit test build directory
$ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=True -DCMAKE_MAKE_PROGRAM=/usr/local/bin/gmake -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-10 -DCMAKE_C_COMPILER=/usr/local/bin/gcc-10
$ gmake VERBOSE=1
$ ctest -v

Can’t thank you enough for the quick response!

Homebrew seems to call the package make even though it’s gmake under the hood. I will take a look at this later today when I’m not stealing time from my day job.

Slight aside, but is there a way to specify the include path(s), and where would I find something like sys/types (either natively or via something like homebrew)? /usr/include is entirely missing on my machine.

What do you mean?

Short answer: no. gcc-10 (installed via brew) should find the include path on its own.

What do you mean?

brew install gmake says it can’t find that package, but brew install make works and actually installs gmake.

And you’re right, the include path was a red herring.

Anyway I did finally get this working.

The first problem, as the helpful folks here indicated, is that it was trying to build using the “fake gcc” that comes with MacOS (in usr/bin). Homebrew installs suffixed versions of the gcc suite in /usr/local/bin, but unless you include the suffix (-10 in my case) it uses the system default which is actually clang/llvm. I tracked down where the Mbed CLI chooses its compilers (in mbed-os/UNITTESTS/unit_test/settings.py) and I added gcc-10, g++-10, etc. to the list of possible compilers to look for.

That still didn’t fix the error in mbed_retarget.h (something about a missing semicolon that is clearly not missing on lines 377, 378, and 379). I eventually gave up and commented out those lines and now the unit tests build and pass (using mbed test --unittests) :man_shrugging:.