PlatformIO Community

Googletest v1.11 build fail on native

Since yesterday, I am hitting issues with unit tests on the Respiraworks project I am working on.
Our unit tests run on native and use googletest with lib_compat_mode = off. It seems something changed yesterday which upgraded googletest to v1.11, and since then unit tests fail to build:

Processing stepper in native environment

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
*** [.pio/build/native/program] Error 1
 [FAILED] Took 2.07 seconds 

Fixing googletest to previous version (v1.10) in platformio.ini fixes the issue:

lib_deps =

I will attempt creating a minimal example for this soon.

Are we talking about the tests in Ventilator/software/common/test at master · RespiraWorks/Ventilator · GitHub?
Also googletest may have changed something in their intenral structure that by-defaults doesn’t define the main() method anymore. I don’t see a main() method being defined directly in the linked test files.

We are using pio 5.2.5

I gave the error for a different test, but yes, all of our tests are all similar to this, with no main() defined, apparently relying on main() defined here: googletest/ at release-1.10.0 · google/googletest · GitHub
My guess is indeed that googletest changed something (though main still looks properly defined in v1.11, but I am not entirely sure ) as those tests run fine with v1.10.

I am mostly intrigued by the fact that this happened yesterday while googletest v1.11 has been out there for almost a year.
Also, pointing out a solution to anyone who faces the same problem, as it took me a while to figure out what happened (especially since it worked fine, until it didn’t, only a few minutes apart, without me noticing that googletest had been updated).

When I look at what I get when executing pio lib -g install "google/googletest@^1.11.0"

I get a library.json that says

  "build": {
    "flags": [
    "srcFilter": [

So the files where the main function would be defined is explictily removed from the build (minus sign)… Let me check the previous version…

The previous 1.10.0 version (pio lib -g install "google/googletest@1.10.0") has

"build": {
        "flags": [
        "srcFilter": [

so here the files are explicitly added with a + sign. I’m not really sure what the dear people at Google have thought here when doing this the opposite way in the 1.11.0 version :confused:.

Can you check whether GitHub - maxgerhardt/Ventilator: Fully-featured ICU ventilator design, optimized for manufacture using commonly available components and free to license. Repository tracks all mechanical, electrical and systems design, software, requirements and regulatory documentation. works better?

You have to implement your own main() function. See new docs GoogleTest — PlatformIO latest documentation

We added support for GMock in the latest release. Now, you are responsible to instruct PlatformIO on which features do you plan to use from GoogleTest:

int main(int argc, char **argv)
    ::testing::InitGoogleTest(&argc, argv);

    // if you plan to use GMock, replace the line above with 
    //::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();

The PlatformIO Core 6.0 is planned for release the next Monday. Could you try the latest RC version using pio upgrade? We would be thankful for your feedback.

Please note that you also need to refactor test folder names. They should start with test_ prefix. You can also group them into a hierarchy with PIO Core 6.0. See Project Structure — PlatformIO latest documentation

Your platformio.ini could look as

platform = native
test_framework = googletest

No need to specify extra libs and flags.

pio upgrade alone doesn’t upgrade to 6.0:

$ pio upgrade
You're up-to-date!
PlatformIO 5.2.5 is currently the newest version available.

I had to run pio upgrade --dev to get PlatformIO has been successfully upgraded to 6.0.0rc2

I will test your proposed solution, but our test suite is pretty big, so I am not sure we will migrate all tests to the new library any time soon.

Thank you both for you quick reply!

You just need to add the test_ prefix to each test folder and update the test’s main source file with the same main() function. It seems that GoogleTest did not change API in the v1.11 version.

Everything seems to work fine with PlatformIO core v6.0(rc2)