PlatformIO Community

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

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.