GoogleTest problem with compilation process

I want to setup project and add possibility to test it with GoogleTest.

  1. I’ve installed GoogleTest library in PIO Home.
  2. I’ve created new project - Board: Arduino Uno and Framework: Arduino
  3. I’ve created file test.cpp in test/native folder and I added:
    #include "gtest/gtest.h"
  4. I’ve added native platform to platformio.ini:
[env:uno]
platform = atmelavr
board = uno
framework = arduino

[env:native]
platform = native

I runned command: platformio.exe test --environment native. The result:

platformio.exe test --environment native <

Verbose mode can be enabled via `-v, --verbose` option
Collected 1 items

Processing native in native environment
---------------------------------------------------------------------------------------------------------------------------------
Building...
test\native\test.cpp:1:10: fatal error: gtest/gtest.h: No such file or directory
 #include "gtest/gtest.h"
          ^~~~~~~~~~~~~~~
compilation terminated.
*** [.pio\build\native\test\native\test.o] Error 1
================================================== [FAILED] Took 2.56 seconds ==================================================

Test    Environment    Status    Duration
------  -------------  --------  ------------
native  uno            IGNORED
native  native         FAILED    00:00:02.555
============================================= 1 failed, 0 succeeded in 00:00:02.555 =============================================
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

I don’t know how to add Gtest library to compilation process. Any suggestions?

Thank You,
szymo

It seems to all be in the include paths…

I had a look at the construction of their own platformio.ini and test code, and to get this compiling, I downloaded the google test repo as a zip file, and put the googlemock and googletest directories at the root of a project (i.e. at the same level as the src and lib directories). I then configured native as

[env:native]
platform = native
build_flags = -Igoogletest/include -Igoogletest

and made a simple dumb main.cpp in test\native\

#include "gtest/gtest.h"

int main()
{
    return 0;
}

which compiled/tested successfully.

I was also able to use this platformio.ini configuration, and avoid having to install the googletest suite manually… but, it’s a bit… convoluted…

[env:native]
platform = native
lib_deps = googletest
build_flags = -I$PROJECT_DIR/.pio/libdeps/$PIOENV/googletest_ID5976/googletest/include -I$PROJECT_DIR/.pio/libdeps/$PIOENV/googletest_ID5976/googletest
1 Like

Yes, it is working - test is compiled. Thank You.
But now I have a problem with intellisense, because “#include “gtest/gtest.h”” is underlined by this one - I think that iostream and others files from c++ aren’t visible for this one. When I’m adding something to the c_cpp_properties.json it is deleted after reset of VSC. #include is also underlined. What is intresting: #include <stdio.h> isn’t underlined.
How can I add files for c++?

Hence the


at the top of the file… :wink:

You’ll probably be after the -Idir build_flags. What is IntelliSense moaning about in the ‘Problems’ tab? That may give some idea as to what stuff it’s having trouble finding.

Yes, I’ve known that this file is generated automatically.
I don’t know how to add files like “iostream” by -Idir (build_flags). I think that these problems are because Intellisense don’t see these files - but compilation proces works great - so the compilator knows these files.

Here my “problem” tab:

Thank You @pfeerick that You are trying to help me :slight_smile:

Is the native environment the first environment in your platformio.ini? Do you get the correct intellisense when you move it upwards?

Native was my second environment. I just changed it to the first one and it seems that the problem is solved. It works and I don’t any problems from intellisense. I’ve never thought that it matters.

Thank You guys for Your help :slight_smile:

1 Like

Only the first environment is used for intellisense, you can’t have both at the same time. (The result could be ambigious…)

1 Like

Yep, you’re right. Now Intellisense for Arduino environment is broken. Thank You again.

Hello @szymo and @maxgerhardt, I have followed the steps mentioned above and reproduce the main.cpp test verbatim and I´m getting this error:

/bin/ld: .pio/build/native/test/native/main.o: in function `(anonymous namespace)::testMath_mySumTest_Test::testMath_mySumTest_Test()':
main.cpp:(.text+0x14): undefined reference to `testing::Test::Test()'
/bin/ld: .pio/build/native/test/native/main.o: in function `(anonymous namespace)::testMath_mySumTest_Test::TestBody()':
main.cpp:(.text+0xc2): undefined reference to `testing::Message::Message()'
/bin/ld: main.cpp:(.text+0xf1): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/bin/ld: main.cpp:(.text+0x104): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/bin/ld: main.cpp:(.text+0x110): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/bin/ld: main.cpp:(.text+0x14d): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/bin/ld: .pio/build/native/test/native/main.o: in function `main':
main.cpp:(.text+0x1dc): undefined reference to `testing::InitGoogleTest(int*, char**)'
/bin/ld: .pio/build/native/test/native/main.o: in function `(anonymous namespace)::testMath_mySumTest_Test::~testMath_mySumTest_Test()':
main.cpp:(.text+0x20e): undefined reference to `testing::Test::~Test()'
/bin/ld: .pio/build/native/test/native/main.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x355): undefined reference to `testing::internal::GetTestTypeId()'
/bin/ld: main.cpp:(.text+0x3c3): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/bin/ld: .pio/build/native/test/native/main.o: in function `RUN_ALL_TESTS()':
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x9): undefined reference to `testing::UnitTest::GetInstance()'
/bin/ld: main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x11): undefined reference to `testing::UnitTest::Run()'
/bin/ld: .pio/build/native/test/native/main.o: in function `testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int)':
main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x71): undefined reference to `testing::internal::IsTrue(bool)'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x96): undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0xfd): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE19GetSetUpCaseOrSuiteEPKci]+0x132): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/bin/ld: .pio/build/native/test/native/main.o: in function `testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int)':
main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x71): undefined reference to `testing::internal::IsTrue(bool)'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x96): undefined reference to `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0xfd): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/bin/ld: main.cpp:(.text._ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci[_ZN7testing8internal16SuiteApiResolverINS_4TestEE22GetTearDownCaseOrSuiteEPKci]+0x132): undefined reference to `testing::internal::GTestLog::~GTestLog()'
/bin/ld: .pio/build/native/test/native/main.o: in function `testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
main.cpp:(.text._ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x47): undefined reference to `testing::AssertionSuccess()'
/bin/ld: .pio/build/native/test/native/main.o: in function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
main.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x8b): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/bin/ld: .pio/build/native/test/native/main.o:(.data.rel.ro+0x20): undefined reference to `testing::Test::SetUp()'
/bin/ld: .pio/build/native/test/native/main.o:(.data.rel.ro+0x28): undefined reference to `testing::Test::TearDown()'
/bin/ld: .pio/build/native/test/native/main.o:(.data.rel.ro+0x68): undefined reference to `typeinfo for testing::Test'

Any idea about why I’m getting this error? Note that with the test:

#include "gtest/gtest.h"

int main()
{
    return 0;
}

I’m having no issues.

Hey @szymo @maxgerhardt @albertotejada

I’m having the same issue as @albertotejada. Headers are found but PIO does not seem to build the googletest libs alongside with the tests, hence the undefined references I guess :confused: any solution available?

Well the problem is that the googletest library has declared

so it will not be seen as a compatible library for the embedded platform with no framework.

Adding lib_compat_mode = off turns that off and it works with no hassle.

[env:native]
platform = native
lib_compat_mode = off
lib_deps = 
    google/googletest @ ^1.10.0

should be your platformio.ini.

See my reference project at GitHub - maxgerhardt/pio-native-gtest-example: Example for the usage of PlatformIO and the googletest library on the native platform. It compiles and executes just fine.

If you don’t want to execute the program yourself but want it to be run when using “Test”, you need to add

test_build_project_src = true ;Build from the sourde code folder

and then “Test” gives

I shared the solution to my problem in this post, check it out and see if it solves also your issue.

1 Like