Unit testing with GTEST/GMOCK on [env:desktop], on Arduino platform

Hi,
I have been trying (desperately, at times), to get the GTEST/GMOCK environment running on Native [env:desktop] with my Arduino project’s Unit Tests because I had some dependencies to mock and GTEST/GMOCK looked like a good idea.
I managed to overcome many hurdles with MINGW install etc. and it is compiling now, but the test won’t execute due to linker issues:

c:/mingw/bin/.[...]./mingw32/bin/ld.exe: c:/[...]/libmingw32.a(mbrscan.o):(.text+0xb6): undefined reference to SetLastError@4'
 c:/mingw/bin/.[...]./mingw32/bin/ld.exe: c:/[...]/libmingw32.a(wcharmap.o):(.text+0x208): undefined reference to WideCharToMultiByte@32'
c:/mingw/bin/.[...]./mingw32/bin/ld.exe: c:/[...]/libmingwex.a(codeset.o):(.text+0xbe): undefined reference to etCPInfo@8
c:/mingw/bin/.[...]./mingw32/bin/ld.exe: c:[...]/libmingwex.a(codeset.o):(.text+0x143): undefined reference to GetCPInfo@8`

I found a similar issue on stackoverflow with no solution that enlightened me.

Here’s my plaformio.ini: (it only works with gnu++11, NOT with c++11)

[env:desktop] 
platform = native
build_flags=
  -std=gnu++11 # c++11# MUST BE gnu++11, not -std=c++11
lib_deps = 
  ArduinoFake
  googletest
lib_compat_mode = off

I checked the SetLastError function, contained in <errno.h> (but this file is not there, instead MINGW is using cerrno, the c++ variant of the c header). It’s in MINGW’s library folder mingw32/9.2.0/include/c++.
What am I missing?

2 Likes

Is your MinGW gcc able to build a simple hello world binary?

Yep.
mingw_HelloWorld
I think something goes wrong when linking GTEST/GMOCK in pio test.

OK, I did a bit of deep-dive into the build output log --verbose (shortened!) attached.
Although UNITY is compiled and linked as well - I am not using it and couldn’t figure out how to disable the tool - I’m sure it does not cause this issue as building with the pio run aborts with the same errors (to compile, I exchanged main.cpp and test_main.cpp temporarily).
Maybe linker order is wrong? Will do some reseach in GTEST/GMOCK’s headers.

I was so unhappy with all my unsuccessful digging that I decided to install Linux as dual boot and try with build-in gcc/g++. I had completely different errors, this time missing -pthread which I had to add to

build_flags =
-std=gnu++11 # must be gnu++11, not -std=c++11 for gtest to run
-pthread # needed to make gtest run on g++

AND - it WORKED :slight_smile: :slight_smile: :slight_smile:
Back to Win10 (I just couldn’t stand it not working on Windows)
I suspected that MinGW was just not configured correctly.

  • Enter command line Start->cmd
  • type mingw-get (pity that I did not do this earlier!)
  • search for mingw32-libmingwex under MinGW Standard Libraries
  • activate both mingw32-libmingwex-dll and mingw32-libmingwex-dev
  • activate mingw32-libpthreadgc-dll and mingw32-libpthreadgc-dev
  • Installation->Apply Changes

Rebuild with pio test -e desktop -f desktop --verbose
Et Voilà:


Should anyone be interested in a tutorial with complete config of platformio and MinGW for googletest, I’d be happy to help.

1 Like

Thank you for this discussion, I just want to point out again that lib_compat_mode = off is necessary and NOT optional, otherwise the code will not compile.

2 Likes

Hi @schallbert I would be very interested in a complete example, a tutorial. Can you write one for the general public?

Sure, will do. I’ll Post it on my page, schallbert.github.io , section Software Projects, within the next couple of days.

1 Like

I really appreciate that! Thank you so much!

Hi @sten, an initial commit just went online on this sub-page. Please let me know if that helps, either here, or in my github discussions channel. I’m happy to receive constructive criticism.

1 Like

Hi @schallbert sounds very promising :slight_smile: Would you mind setting up discussions for the repo? I think that my feedback might be better placed right there.
For a start: in “pio run -e desktop -f desktop” the “-f” switch is unrecognized. It is not a part of the “pio run” interface. Is there something missing that points to gtest?

Oops, that’s actually a mistake. Will correct it. The line is:
pio test -e desktop -f desktop
Here’s the fresh discussions page: Project discussion

The tuturial works just fine using your example repo. Many thanks for that.

1 Like