PlatformIO Community

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?

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.BuildOutput--verbose

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à:
gtest-success!
Should anyone be interested in a tutorial with complete config of platformio and MinGW for googletest, I’d be happy to help.