How to resolve the dependency of glog on gtest during cmake configuration time?

527 Views Asked by At

So I am trying to use CMake to download and configure both gtest and then glog, for my project. When downloading them separately, compiling and linking, that works fine.

However, I would like to be able to configure the project accurately during the configuration time.

The problem is that glog during configuration time wants to find gtest, which is not build yet.

-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) 
CMake Warning at CMakeLists.txt:78 (find_package): By not providing "Findgflags.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "gflags", but CMake did not find one.

The way I am currently doing is using the method mentioned here for both gtest 1st and glog 2nd, (https://crascit.com/2015/07/25/cmake-gtest/), in that order.

When I build glog when gtest is not found I get a bunch of errors like "error MSB3073 : The command setlocal", "error C2065: 'snprintf': undeclared identifier" and "error C2375: 'snprintf': redefinition; different linkage".

Please let me know if you have any advice, thank you.

Edit 1 :

gtest is version 1.11.0

glog is version 0.5.0

Edit 2 :

Adding a small code sample, to show to replicate the issue.

Below is the project structure.

Project
├───CMakeLists.txt (A)
└───external
     ├───CMakeLists.txt (B)
     ├───tester
     |    ├───CMakeLists_Google_Tester.in
     |    └───CMakeLists.txt (C)
     └───logger
          ├───CMakeLists_Google_Logger.in
          └───CMakeLists.txt (D)

Below is CMakeLists.txt (A).

cmake_minimum_required(VERSION 3.20.2)
project(SampleProject)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
include(CTest)
# External projects.
add_subdirectory(external)

Below is CMakeLists.txt (B).

cmake_minimum_required(VERSION 3.20.2)
add_subdirectory(tester)
add_subdirectory(logger)

Below is CMakeLists.txt (C).

cmake_minimum_required(VERSION 3.20.2)
project(ExternalTesterProject NONE)

configure_file(CMakeLists_Google_Tester.in external_tester_download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_tester_download")
execute_process(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_tester_download")

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/external_tester_src"
                 "${CMAKE_CURRENT_BINARY_DIR}/external_tester_build")

Below is CMakeLists.txt (D).

cmake_minimum_required(VERSION 3.20.2)
project(ExternalLoggerProject NONE)

configure_file(CMakeLists_Google_Logger.in external_logger_download/CMakeLists.txt)
execute_process(
    COMMAND "${CMAKE_COMMAND}" . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_logger_download")
execute_process(
    COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/external_logger_download")

add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/external_logger_src"
                 "${CMAKE_CURRENT_BINARY_DIR}/external_logger_build")

Below is CMakeLists_Google_Logger.in .

cmake_minimum_required(VERSION 3.20.2)
project(ExternalLoggerProject NONE)

include(ExternalProject)
ExternalProject_Add(external_logger
    GIT_REPOSITORY "https://github.com/google/glog.git"
    GIT_TAG "v0.5.0"
    SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_logger_src"
    BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_logger_build"
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    TEST_COMMAND "")

Below is CMakeLists_Google_Tester.in .

cmake_minimum_required(VERSION 3.20.2)

project(ExternalTesterProject NONE)

include(ExternalProject)
ExternalProject_Add(external_tester
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG release-1.11.0
    SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_tester_src"
    BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/external_tester_build"
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    TEST_COMMAND "")
0

There are 0 best solutions below