1

我正在迈出第一步CMake,我有一个 Hello World 示例,我想在其中使用libgit2。我的目录结构如下:

- main.cpp
- CMakeLists.txt
- cmake/Modules/Findlibgit2.cmake
- libs/libgit2/include/git2.h
- libs/libgit2/debug/git/git2.lib
- libs/libgit2/debug/git/git2.dll

我想静态或动态地链接它,至少它在开始时有效。但是当我执行 CMake 时,我收到下面的错误消息。谁能帮我解决这个问题?

Not searching for unused variables given on the command line.
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:164 (message):
[cmake]   Could NOT find libgit2 (missing: GIT2_LIBRARY GIT2_INCLUDE_PATH)
[cmake] Call Stack (most recent call first):
[cmake]   C:/Program Files/CMake/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:445 (_FPHSA_FAILURE_MESSAGE)
[cmake]   cmake/Modules/Findlibgit2.cmake:15 (find_package_handle_standard_args)
[cmake]   CMakeLists.txt:6 (find_package)

Findlibgit2.cmake

# Find git2 Library
#
#  GIT2_INCLUDE_DIRS - where to find git2.h, etc.
#  GIT2_LIBRARIES    - List of libraries when using libgit2.
#  GIT2_FOUND        - True if libgit2 is found.

# GIT2_INCLUDE_PATH
find_path(GIT2_INCLUDE_PATH NAMES git2.h)
# GIT2_LIBRARY
find_library(GIT2_LIBRARY NAMES git2)

# handle the QUIETLY and REQUIRED arguments and set GIT2_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(libgit2 REQUIRED_VARS GIT2_LIBRARY GIT2_INCLUDE_PATH)


if (GIT2_FOUND)
  set(GIT2_INCLUDE_DIR  ${GIT2_INCLUDE_PATH})
  set(GIT2_INCLUDE_DIRS ${GIT2_INCLUDE_PATH})
  set(GIT2_LIBRARIES    ${GIT2_LIBRARY})
endif()

mark_as_advanced(
  GIT2_INCLUDE_PATH
  GIT2_LIBRARY
)

CMakeLists.txt


cmake_minimum_required(VERSION 2.8.9)
project (backend)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
find_package(libgit2 REQUIRED)

include_directories(${LIBGIT2_INCLUDE_DIR})
link_directories("backend/libs/libgit2/debug")
add_executable(backend main.cpp)
4

1 回答 1

1

似乎 libgit2 没有提供 CMake包配置文件,因此由您自己编写查找模块或在 Internet 上查找模块。您拥有的Findlibgit2.cmake文件是一个好的开始。

我在这里找到了另一个 libgit2 Find Module:Findlibgit2.cmake。这个使用pkg-config,利用 libgit2 本身提供的 .pc 文件,但是,考虑到您使用的是 Windows,需要您手动安装 pkg-config。这可能比它的价值更麻烦。

关于您的错误信息:

除非 libgit2 包在标准系统路径中,否则 CMake 将不知道在哪里寻找它。所以,你必须明确告诉 CMake 去哪里找。一种简单的方法是修改Findlibgit2.cmake文件,使用HINTS命令find_*

# GIT2_INCLUDE_PATH
find_path(GIT2_INCLUDE_PATH NAMES git2.h
    HINTS ${GIT2_INCLUDEDIR}
)
# GIT2_LIBRARY
find_library(GIT2_LIBRARY NAMES git2
    HINTS ${GIT2_LIBRARYDIR}
)

现在,我们可以填充这些GIT2_INCLUDEDIRGIT2_LIBRARYDIR变量以指向已安装的 libgit2 包。这样做之后,find_package()应该会成功找到包组件,并且可以链接到git2.lib使用${GIT2_LIBRARIES}.

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.9)
project (backend)

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")

# Set hints so CMake knows where libgit2 is located on your machine.
set(GIT2_INCLUDEDIR ${CMAKE_CURRENT_LIST_DIR}/libs/libgit2/include)
set(GIT2_LIBRARYDIR ${CMAKE_CURRENT_LIST_DIR}/libs/libgit2/debug/git)
find_package(libgit2 REQUIRED)

include_directories(${GIT2_INCLUDE_DIR})

# Don't need this line. Use of 'link_directories' is discouraged.
link_directories("backend/libs/libgit2/debug")

add_executable(backend main.cpp)
# Add this line to link against the libgit2 libraries.
target_link_libraries(backend PRIVATE ${GIT2_LIBRARIES})

设置这些变量的一种更便携的方法是在命令行上HINTS调用时设置它们:cmake

cmake -DGIT2_INCLUDEDIR=C:/path/to/libgit2/include -DGIT2_LIBRARYDIR=D:/path/to/libgit2/lib ..

这样,如果您有其他开发人员在使用该项目,他们可以指向他们机器上可能安装了 libgit2 的任何位置,从而无需修改 CMakeLists.txt 文件。

于 2020-07-17T18:23:37.310 回答