如何通过cmake包含libgit2

时间:2016-02-12 23:24:51

标签: cmake libgit2

如何将libgit2作为cmake中的库包含在内。 我从github克隆了libgit2存储库。 比我跑

mkdir build && cd build
cmake ..
make
sudo make install

比我在/ usr / local / include / git2 /和/ usr / local / include下找到已安装的文件。

我现在需要在cmake文件中添加什么内容?

2 个答案:

答案 0 :(得分:1)

以下是一个为您自己的项目定制的示例:

将以下内容添加到您的顶级CMakeLists.txt:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
find_package(git2 REQUIRED)
include_directories(${GIT2_INCLUDE_DIR})

add_executable(lg2 main.c) # can be add_library, etc.

target_link_libraries(lg2 ${GIT2_LIBRARIES})

创建一个名为cmake的目录,然后在其中创建一个名为Findgit2.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
)

答案 1 :(得分:1)

FWIW我会将libgit2构建为您项目的一部分,如下所示:

# Set libgit2 options (e.g. disable shared lib)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
...

# Build libgit2
add_subdirectory(path/to/libgit2)

# Add your own exe or library target
add_executable(my_exe ...)

# Link libgit2 into your exe or library
target_link_libraries(my_exe git2 ...)