c++cmakecross-compilinggoogletestundeclared-identifier

(How) can I compile googletest for a non-POSIX target?


I'm in a cross-compiling environment where my project needs to be compiled for Linux, Windows, and a custom target, which is cross-compiled on Windows.

The project uses googletest for unit testing, and is included as follows, in a CMake find-file, as follows:

# FindGTest.cmake
if (CMAKE_SYSTEM_NAME STREQUAL "TheTarget")
  FetchContent_Declare(
      GTest
      GIT_REPOSITORY https://github.com/google/googletest.git
      GIT_TAG v1.14.0)
  FetchContent_MakeAvailable(GTest)
else()
    find_package(GTest CONFIG REQUIRED)
endif()

The Windows and Linux builds are not cross-compiled, and they compile fine. But for the custom target, I get the following compile errors (edited for readability):

C:/dev/project/build/_deps/gtest-src/googletest/include\gtest/internal/gtest-port.h:2058:38: error: use of undeclared identifier 'isatty'
...
C:/dev/project/build/_deps/gtest-src/googletest/include\gtest/internal/gtest-port.h:2087:44: error: use of undeclared identifier 'chdir'
...
C:/dev/project/build/_deps/gtest-src/googletest/include\gtest/internal/gtest-port.h:2135:10: error: use of undeclared identifier 'getenv'; did you mean 'GetEnv'?
...
C:/dev/project/_deps/gtest-src/googletest\src/gtest-death-test.cc:1106:27: error: use of undeclared identifier 'pipe'
...
C:/dev/project/build/_deps/gtest-src/googletest\src/gtest-death-test.cc:1119:27: error: use of undeclared identifier 'fork'
...
C:/dev/project/build/_deps/gtest-src/googletest\src/gtest-death-test.cc:1204:3: error: use of undeclared identifier 'execv'
...
C:/dev/project/build/_deps/gtest-src/googletest\src/gtest-filepath.cc:115:18: error: use of undeclared identifier 'getcwd'

How can I get past these compile errors for my cross-compile target?
Is there a way to do this by supplying the right combination of macros to the build (e.g. with CMake add_definition() statements)?
Or must I create a custom version of gtest? I.e. I imagine this means I should not use FetchContent_Declare()/FetchContent_MakeAvailable() or find_package() to include googletest, but rather I should have googletest as a subdirectory of my project, possibly with code changes to work around the use of the undefined functions(?)


Solution

  • These errors and a few others

    googletest\src/gtest-death-test.cc:1119:27: error: use of undeclared identifier 'fork'
    googletest\src/gtest-death-test.cc:1204:3: error: use of undeclared identifier 'execv'
    

    can be resolved by disabling the support of death tests, patching googletest/include/gtest/internal/gtest-port.h and undefining the macro GTEST_HAS_DEATH_TEST for your custom target. Modify the condition

    #if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS ||             \
         (GTEST_OS_MAC && !GTEST_OS_IOS) ||                                   \
         (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER) || GTEST_OS_WINDOWS_MINGW ||  \
         GTEST_OS_AIX || GTEST_OS_HPUX || GTEST_OS_OPENBSD || GTEST_OS_QNX || \
         GTEST_OS_FREEBSD || GTEST_OS_NETBSD || GTEST_OS_FUCHSIA ||           \
         GTEST_OS_DRAGONFLY || GTEST_OS_GNU_KFREEBSD || GTEST_OS_HAIKU)
    # define GTEST_HAS_DEATH_TEST 1
    #endif
    

    For rest errors, you may implement a minimal POSIX like MinGW does it.