I am trying to add unit testing to my project and I can’t get it to build. I’m on Windows and I’m using Mbed 6.15 with the cli2.
I created the project by
mbed-tools new
mbed-tools configure -m K64F -t GCC_ARM
Then in my root CMakeList.txt I added the following 3 lines.
add_subdirectory(${MBED_PATH}/UNITTESTS) # mbed os unit test folder to include gtest
include(CTest)
add_subdirectory(UNITTESTS) # my unit test folder with my unit tests
Next I created a UNITTEST folder and in it I have a CMakeList.txt like this
add_executable(
hello_test
test_MyClass.cpp
)
target_link_libraries(
hello_test
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)
and a test_MyClass.cpp file like this
#include <gtest/gtest.h>
class TestmyClass : public testing::Test {
virtual void SetUp() {
}
virtual void TearDown() {
}
};
// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
I run the first cmake command with no issue
cmake -S . -B cmake_build\K64F\develop\GCC_ARM -GNinja -DBUILD_TESTING=ON
and then when I run the actual build command it fails
cmake --build cmake_build\K64F\develop\GCC_ARM
with this error
[8/16] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
FAILED: _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
C:\ProgramData\chocolatey\bin\ccache.exe C:\PROGRA~2\GNUARM~1\102021~1.10\bin\AR10B2~1.EXE @_deps\googletest-build\googletest\CMakeFiles\gtest_main.dir\src\gtest_main.cc.obj.rsp -MD -MT _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj -MF _deps\googletest-build\googletest\CMakeFiles\gtest_main.dir\src\gtest_main.cc.obj.d -o _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj -c C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest_main.cc
In file included from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest-message.h:55,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest-assertion-result.h:46,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest.h:59,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest_main.cc:32:
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2015:40: error: 'fileno' was not declared in this scope; did you mean 'file'?
2015 | inline int FileNo(FILE* file) { return fileno(file); }
| ^~~~~~
| file
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'char* testing::internal::posix::StrDup(const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2021:47: error: 'strdup' was not declared in this scope; did you mean 'StrDup'?
2021 | inline char* StrDup(const char* src) { return strdup(src); }
| ^~~~~~
| StrDup
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2065:56: error: 'fdopen' was not declared in this scope; did you mean 'fopen'?
2065 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
| ^~~~~~
| fopen
[9/16] Building CXX object _deps/googletest-build/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj
FAILED: _deps/googletest-build/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj
C:\ProgramData\chocolatey\bin\ccache.exe C:\PROGRA~2\GNUARM~1\102021~1.10\bin\AR10B2~1.EXE @_deps\googletest-build\googlemock\CMakeFiles\gmock_main.dir\src\gmock_main.cc.obj.rsp -MD -MT _deps/googletest-build/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj -MF _deps\googletest-build\googlemock\CMakeFiles\gmock_main.dir\src\gmock_main.cc.obj.d -o _deps/googletest-build/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj -c C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/src/gmock_main.cc
In file included from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/internal/gmock-port.h:57,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:49,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/gmock-actions.h:145,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/gmock.h:56,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/src/gmock_main.cc:32:
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2015:40: error: 'fileno' was not declared in this scope; did you mean 'file'?
2015 | inline int FileNo(FILE* file) { return fileno(file); }
| ^~~~~~
| file
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'char* testing::internal::posix::StrDup(const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2021:47: error: 'strdup' was not declared in this scope; did you mean 'StrDup'?
2021 | inline char* StrDup(const char* src) { return strdup(src); }
| ^~~~~~
| StrDup
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2065:56: error: 'fdopen' was not declared in this scope; did you mean 'fopen'?
2065 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
| ^~~~~~
| fopen
[10/16] Building CXX object _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.obj
FAILED: _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.obj
C:\ProgramData\chocolatey\bin\ccache.exe C:\PROGRA~2\GNUARM~1\102021~1.10\bin\AR10B2~1.EXE @_deps\googletest-build\googlemock\CMakeFiles\gmock.dir\src\gmock-all.cc.obj.rsp -MD -MT _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.obj -MF _deps\googletest-build\googlemock\CMakeFiles\gmock.dir\src\gmock-all.cc.obj.d -o _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.obj -c C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/src/gmock-all.cc
In file included from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/internal/gmock-port.h:57,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h:49,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/gmock-actions.h:145,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/include/gmock/gmock.h:56,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googlemock/src/gmock-all.cc:39:
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2015:40: error: 'fileno' was not declared in this scope; did you mean 'file'?
2015 | inline int FileNo(FILE* file) { return fileno(file); }
| ^~~~~~
| file
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'char* testing::internal::posix::StrDup(const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2021:47: error: 'strdup' was not declared in this scope; did you mean 'StrDup'?
2021 | inline char* StrDup(const char* src) { return strdup(src); }
| ^~~~~~
| StrDup
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2065:56: error: 'fdopen' was not declared in this scope; did you mean 'fopen'?
2065 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
| ^~~~~~
| fopen
[11/16] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
FAILED: _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
C:\ProgramData\chocolatey\bin\ccache.exe C:\PROGRA~2\GNUARM~1\102021~1.10\bin\AR10B2~1.EXE @_deps\googletest-build\googletest\CMakeFiles\gtest.dir\src\gtest-all.cc.obj.rsp -MD -MT _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj -MF _deps\googletest-build\googletest\CMakeFiles\gtest.dir\src\gtest-all.cc.obj.d -o _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj -c C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest-all.cc
In file included from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest-message.h:55,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest-assertion-result.h:46,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/gtest.h:59,
from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest-all.cc:38:
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2015:40: error: 'fileno' was not declared in this scope; did you mean 'file'?
2015 | inline int FileNo(FILE* file) { return fileno(file); }
| ^~~~~~
| file
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'char* testing::internal::posix::StrDup(const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2021:47: error: 'strdup' was not declared in this scope; did you mean 'StrDup'?
2021 | inline char* StrDup(const char* src) { return strdup(src); }
| ^~~~~~
| StrDup
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h: In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h:2065:56: error: 'fdopen' was not declared in this scope; did you mean 'fopen'?
2065 | inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
| ^~~~~~
| fopen
In file included from C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest-all.cc:45:
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest-port.cc: In constructor 'testing::internal::CapturedStream::CapturedStream(int)':
C:/Users/username/Desktop/gtest_example/cmake_build/K64F/develop/GCC_ARM/_deps/googletest-src/googletest/src/gtest-port.cc:1102:31: error: '::mkstemp' has not been declared
1102 | const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data()));
| ^~~~~~~
ninja: build stopped: subcommand failed.