Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Can't use libgit2 as a CMake subproject together with googletest

未关闭
#7,162 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
c, cmake
领域
build-system

调研方向

使用 CMake 3.28.3 重现两个 FetchContent 配置,然后检查 cmake/FindStatNsec.cmake 以及 CMakeConfigureLog.yaml 中与失败检查对应的条目。当在前面添加 googletest 后,libgit2 能够成功配置和构建,且不会出现错误的 -std=c90 检查失败时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

I'm using CMake 3.28.3 and GCC 13.3.0 on Ubuntu 24.04 on amd64. I'm trying to use libgit2 1.9.1 as a subproject of my own project.

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.28.3)

project(myProject VERSION 0.0.1 LANGUAGES C)

include(FetchContent)

FetchContent_Declare(
  libgit2
  GIT_REPOSITORY https://github.com/libgit2/libgit2.git
  GIT_TAG        0060d9cf5666f015b1067129bd874c6cc4c9c7ac  # v1.9.1
)
FetchContent_MakeAvailable(libgit2)

add_library(lib)
target_sources(lib PRIVATE lib.c)
target_link_libraries(lib PRIVATE libgit2)

When I run cmake -B build in my project's directory, cmake/FindStatNsec.cmake from libgit2 will call

check_struct_has_member("struct stat" st_mtim "sys/types.h;sys/stat.h"
        HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C)

CMake will create a C file that references struct stat's st_mtim member variable and compile it with no special compiler flags. On my system, this is successful:

        /usr/bin/cc -DHAVE_STRUCT_STAT_ST_MTIM   -o CMakeFiles/cmTC_2a383.dir/src.c.o -c /app/build/CMakeFiles/CMakeScratch/TryCompile-3aEPgD/src.c

(taken from build/CMakeFiles/CMakeConfigureLog.yaml)

However when I add googletest before libgit2 like this:

cmake_minimum_required(VERSION 3.28.3)

project(myProject VERSION 0.0.1 LANGUAGES C)

include(FetchContent)

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG        52eb8108c5bdec04579160ae17225d66034bd723  # v1.17.0
)
FetchContent_MakeAvailable(googletest)

FetchContent_Declare(
  libgit2
  GIT_REPOSITORY https://github.com/libgit2/libgit2.git
  GIT_TAG        0060d9cf5666f015b1067129bd874c6cc4c9c7ac  # v1.9.1
)
FetchContent_MakeAvailable(libgit2)

add_library(lib)
target_sources(lib PRIVATE lib.c)
target_link_libraries(lib PRIVATE libgit2)

then the check for st_mtim will fail because the compiler will be called with -std=c90:

        /usr/bin/cc -DHAVE_STRUCT_STAT_ST_MTIM  -std=c90 -o CMakeFiles/cmTC_99f3b.dir/src.c.o -c /app/build/CMakeFiles/CMakeScratch/TryCompile-df2ogP/src.c
        /app/build/CMakeFiles/CMakeScratch/TryCompile-df2ogP/src.c: In function 'main':
        /app/build/CMakeFiles/CMakeScratch/TryCompile-df2ogP/src.c:7:36: error: 'struct stat' has no member named 'st_mtim'; did you mean 'st_mtime'?
            7 |   (void)sizeof(((struct stat *)0)->st_mtim);
              |                                    ^~~~~~~
              |                                    st_mtime

Building my project with cmake --build build will fail with

[  1%] Building C object _deps/libgit2-build/src/libgit2/CMakeFiles/libgit2.dir/annotated_commit.c.o
In file included from /app/build/_deps/libgit2-src/src/util/posix.h:157,
                 from /app/build/_deps/libgit2-src/src/util/git2_util.h:111,
                 from /app/build/_deps/libgit2-src/src/libgit2/common.h:10,
                 from /app/build/_deps/libgit2-src/src/libgit2/annotated_commit.h:10,
                 from /app/build/_deps/libgit2-src/src/libgit2/annotated_commit.c:8:
/app/build/_deps/libgit2-src/src/util/unix/posix.h:35:3: error: #error GIT_USE_NSEC defined but unknown struct stat nanosecond type
   35 | # error GIT_USE_NSEC defined but unknown struct stat nanosecond type
      |   ^~~~~
cc1: note: unrecognized command-line option '-Wno-documentation-deprecated-sync' may have been intended to silence earlier diagnostics
gmake[2]: *** [_deps/libgit2-build/src/libgit2/CMakeFiles/libgit2.dir/build.make:76: _deps/libgit2-build/src/libgit2/CMakeFiles/libgit2.dir/annotated_commit.c.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:642: _deps/libgit2-build/src/libgit2/CMakeFiles/libgit2.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

I haven't looked further into this yet. It might as well be a googletest bug and not a libgit2 bug.

主要语言
C
星标
10.6k
派生
2.7k
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

libgit2/libgit2 的其他 Issue

查看 libgit2/libgit2 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。