harbor run -t LiteCoder/cmake-crossplatform-gtest-setupharbor download LiteCoder/cmake-crossplatform-gtest-setupSet up a cross-platform CMake build system that automatically fetches and compiles GoogleTest (v1.12.0), links it correctly, and compiles a simple unit test executable for both Linux and Windows (via MinGW cross-compiler) using a single CMakeLists.txt file.
Create the following directory structure under /app/:
/app/
├── CMakeLists.txt
├── build.sh
├── mingw-toolchain.cmake
├── src/
│ └── mathlib.cpp
├── include/
│ └── mathlib.h
└── tests/
└── test_mathlib.cpp
FetchContent modulex86_64-w64-mingw32-g++)MathLibsrc/mathlib.cpp and include/mathlib.h)Create a simple C++ library in a namespace called mathlib that provides at least the following functions:
int add(int a, int b) — returns the sum of two integersint subtract(int a, int b) — returns the difference (a - b)int multiply(int a, int b) — returns the product of two integersdouble divide(double a, double b) — returns the quotient (a / b); throws std::invalid_argument if b is zeroThe header file must use an include guard. The library must be built as a static library target named mathlib.
tests/test_mathlib.cpp)Write GoogleTest-based unit tests that cover:
std::invalid_argumentThe test executable target must be named mathlib_tests. Tests must be registered with add_test() via CMake's enable_testing() so they can be run with ctest.
The single top-level /app/CMakeLists.txt must:
MathLibFetchContent to download GoogleTest at tag release-1.12.0 from https://github.com/google/googletest.gitmathlib from sources in src/ with headers in include/mathlib_tests from tests/test_mathlib.cpp, linked against both mathlib and GTest::gtest_mainenable_testing() and register the test executable with add_test()mingw-toolchain.cmake)Create a CMake toolchain file at /app/mingw-toolchain.cmake that:
CMAKE_SYSTEM_NAME to WindowsCMAKE_C_COMPILER to x86_64-w64-mingw32-gccCMAKE_CXX_COMPILER to x86_64-w64-mingw32-g++CMAKE_FIND_ROOT_PATH_MODE_PROGRAM to NEVERCMAKE_FIND_ROOT_PATH_MODE_LIBRARY and CMAKE_FIND_ROOT_PATH_MODE_INCLUDE to ONLYbuild.sh)Create an executable bash script at /app/build.sh that automates both build configurations:
/app/build-linux/, then run tests via ctest --test-dir /app/build-linux//app/build-windows/ using the MinGW toolchain file (-DCMAKE_TOOLCHAIN_FILE=../mingw-toolchain.cmake)The script must:
#!/bin/bash shebangset -e)cmake configures successfully for the Linux build in /app/build-linux/make (or cmake --build) compiles the Linux build without errorsctest in the Linux build directory runs all tests and they passcmake configures successfully for the Windows cross-compilation build in /app/build-windows/ using the MinGW toolchainmake (or cmake --build) compiles the Windows cross-compilation build without errorsmathlib_tests.exe executable in /app/build-windows/ (or a subdirectory thereof)