ThomazGaio
ThomazGaio

Reputation: 71

How to set GoogleTest variables GTEST_LIBRARY GTEST_INCLUDE_DIR and GTEST_MAIN_LIBRARY to CMake on Windows?

There is a project that was developed for linux environment. Now I am trying to build this on windows using CMake.

I keep trying to build the project and always get this error:

CMake Error at C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
Call Stack (most recent call first):
C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files(x86)/CMake/share/cmake-3.3/Modules/FindGTest.cmake:204 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
C:/Users/awy9/git/cmake_modules/modules/SigeoGTest.cmake:21 (find_package)  
C:/Users/awy9/git/cmake_modules/modules/SigeoInit.cmake:29 (include)  
CMakeLists.txt:12 (include)

How can I set these variables to work on this project now on Windows?

Upvotes: 4

Views: 25493

Answers (3)

alextoind
alextoind

Reputation: 1203

I've come up with a similar issue when building my project through GitHub Actions on Windows platform.

Even if I export the CMAKE_PREFIX_PATH to enable the find_package(GTest REQUIRED) to properly find the framework library (Windows is a bit nasty BTW), it continues to fail with Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY). Note that it correctly fetches the include directory.

After digging a little bit deeper into the issue I realized that on Windows the command cmake --build . --target install without any --config options, builds the library in Debug configuration, contrary to Linux and macOS.

If you are then building your project in Release, e.g. cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release --target install, the find_package(GTest REQUIRED) won't find any GTest library because it searches for the nonexistent Release build.

In this case a better solution is to install GTest in Release mode w.r.t. hardcoding the library paths to fetch the Debug\ directory builds.

I attach also the GitHub Action steps which may be helpful for someone:

    steps:
    - name: Clone GTest
      shell: bash
      working-directory: ${{runner.workspace}}
      run: git clone https://github.com/google/googletest.git

    - name: Create GTest Build Environment
      shell: bash
      working-directory: ${{runner.workspace}}/googletest
      run: cmake -E make_directory build

    - name: Configure GTest CMake
      shell: bash
      working-directory: ${{runner.workspace}}/googletest/build
      run: cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_FLAGS=-std=c++11

    - name: Install GTest
      shell: bash
      working-directory: ${{runner.workspace}}/googletest/build
      run: cmake --build . --config $BUILD_TYPE --target install || sudo cmake --build . --config $BUILD_TYPE --target install

    - name: Export CMAKE_PREFIX_PATH on Windows
      shell: bash
      if: runner.os == 'Windows'
      run: echo '::set-env name=CMAKE_PREFIX_PATH::C:/Program Files (x86)/googletest-distribution'

    - name: Checkout
      uses: actions/checkout@v2

    - ... your project build steps

Upvotes: 0

ffarhour
ffarhour

Reputation: 352

Honestly It's not the best idea to set hardcoded variables like you've done with GTEST_LIBRARY and GTEST_MAIN_LIBRARY.

I had the exact same problem. I was using gtest 1.7.0 on windows and getting the same error. Turns out "GTest" should be "gtest" (not capitalized).

So the code in my CMakeLists.txt became:

find_package(gtest REQUIRED)

Note that I was using CMake 3.9


EDIT

I would highly recommend following this guide now:

http://crascit.com/2015/07/25/cmake-gtest/

It is by far the best method I have found to use unbuilt gtest and gmock in your cmake project.

Upvotes: 4

ThomazGaio
ThomazGaio

Reputation: 71

UPDATE

@Tsyvarev helped me solving the first problem and I quote him:

This is standard CMake message, when requested package(GTest in your case) is not found. You should install GTest before configuring your project. Or, if you already have installed it into non-standard location, pass this location via GTEST_ROOT variable: cmake -DGTEST_ROOT= ...

As alternative for the passing variable to cmake, you can set environment variable: set GTEST_ROOT=

This solved the problem with the value of GTEST_INCLUDE_DIR, but I still had these errors:

CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.3/Modules/FindPackageH andleStandardArgs.cmake:148 (message): Could NOT find GTest (missing: GTEST_LIBRARY GTEST_MAIN_LIBRARY)

SOLUTION

I figured that on Windows environment some values are different from what the manuals say.

After compiling Gtest, I got two libs: gtest.lib and gtest_main.lib Then, I set these filepath variables:

GTEST_LIBRARY=C:\Users\awy9\Softwares\Gtest\gtest-1.7.0\Debug\gtest.lib GTEST_MAIN_LIBRARY=C:\Users\awy9\Softwares\Gtest\gtest-1.7.0\Debug\gtest_main.lib

Now everything is working!

Thank you

Upvotes: 3

Related Questions