Search code examples
c++arduinogoogletest

How to fix "undefined reference to:" in arduino ide, trying to use googletest


I am trying to use the googletest library to test code in the arduino ide, and I keep getting errors such as

In function __static_initialization_and_destruction_0' undefined reference to `std::ios_base::Init::Init()'

There are 50 more lines of this error, including

undefined reference to testing::Test::SetUp()' undefined reference to testing::Test::TearDown()

Since even the basic test functions are failing, I assume I made a mistake in where I put my library files, or perhaps I'm simply not including the correct files.

Currently I am only including gtest/gtest.h, as that is all I have seen in other examples in order for the code to work.

I have built the googletest library using cmake and make, and included it within the project.

Does anyone have an idea of what I can do to fix this?


Solution

  • You can't compile googletest in the Arduino IDE. Primarily because the compiler used by the Arduino IDE is meant to generate (for normal arduinos) AVR binaries not x86/x86-64 ones. These AVR binaries can't be ran on a normal PC and are only meant to be ran on the embedded platform. Because on the embedded platform the normal standard text IO streams are not available the arduino IDE doesn't include the standard IO used on normal PC's for terminal use. (aka pipes 0 1 2 on unix).

    I have built the googletest library using cmake and make, and included it within the project.

    It is correct that this isn't going to work. If you compile googletest with cmake/make you are probably building a x86 or x86-64 binary which you are then including in the arduino IDE. The arduino IDE compiler is then going to try to link an AVR binary to a x86/x86-64 library. This is not going to work.

    I would recommend you design your software in such a way that the hardware dependent logic is removed from your business logic if you do want to test your Arduino code with googletest. The business logic can then be unit tested by compiling a ('normal') x86 or x86-64 binary with the test code which can be ran on a normal pc. This will involve mocking up the hardware interfaces used in your code. This build infrastructure can easily be set up inside the same structure of your arduino project and testing will only involve building a test binary and running that.