【发现一个问题】cmake中通过FetchContent模式来下载gtest库会导致循环执行

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


cmake 中使用了如下代码:

include(FetchContent)

FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
)

FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
  FetchContent_Populate(googletest)

  # 打印路径检查(核心保护)
  message(STATUS "GTest Source Dir: ${googletest_SOURCE_DIR}")
  message(STATUS "GTest Binary Dir: ${googletest_BINARY_DIR}")

  if(${googletest_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
    message(FATAL_ERROR "GTest SOURCE_DIR 回到了主工程目录,导致循环!")
  endif()

  add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR})
endif()

运行 cmake 时发现在循环执行:

You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_C_COMPILER= /usr/bin/cc

-- The C compiler identification is GNU 8.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Os name: Linux
-- Os name: x86_64
-- GTest Source Dir: /home/fuchun.zhang/code/code.byted.org/fuchun.zhang/libextractvideoframe/build/linux/_deps/googletest-src
-- GTest Binary Dir: /home/fuchun.zhang/code/code.byted.org/fuchun.zhang/libextractvideoframe/build/linux/_deps/googletest-build
-- The CXX compiler identification is GNU 8.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: /usr/bin/python3.7 (found version "3.7.3") found components:  Interpreter 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done

上述的文本循环出现,程序不会停止。

暂时放弃了这个方法。

posted on 2025-03-20 17:41  ahfuzhang  阅读(71)  评论(0)    收藏  举报