c++ - 在cmake项目中构建libgit2

标签 c++ c git cmake libgit2

我有下一个项目树:

>googletest
>libgit2
>src
>tests

主CMakeLists.txt中的代码:

cmake_minimum_required(VERSION 2.8)
project(project_name)
add_subdirectory(src)
add_subdirectory(tests)

src文件夹中的代码CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(project_name)

set(libgit2_src ../libgit2/src)
include_directories(../libgit2 ../libgit2/include)

add_library(gitlib STATIC ${libgit2_src})

add_executable(project_name main.cpp)
target_link_libraries(project_name gitlib)

当我尝试生成项目时,出现下一个错误:

CMake Error: CMake can not determine linker language for target: gitlib

如何在我的项目中正确构建和包含库 libgit2?

编辑

在主 CMakeLists.txt 中,我添加了下一个命令:

cmake_minimum_required(VERSION 2.8)

project(project_name)

add_subdirectory(libgit2)
add_subdirectory(src)
add_subdirectory(tests)

之后,我更改了 src 文件夹中的代码 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(project_name)

pkg_check_modules(LIBGIT2 REQUIRED libgit2)

set(LIBGIT2_LIBS_ABSOLUTE)
foreach(lib ${LIBGIT2_LIBRARIES})
    # Choose name of variable, which will contain result of `find_library`
    # for specific library.
    set(var_name LIBGIT2_${lib}_ABS)
    # Search library under dirs, returned by pkg-config.
    find_library(${var_name} ${lib} ${LIBGIT2_LIBRARY_DIR})
    list(APPEND LIBGIT2_LIBS_ABSOLUTE ${${var_name}})
endforeach()

include_directories(${LIBGIT2_INCLUDE_DIRS})

add_executable(project_name main.cpp)
target_link_libraries(project_name ${LIBGIT2_LIBS_ABSOLUTE}) 

但是当我配置项目时,我收到错误:

CMake Error at D:/Program/CMake/share/cmake-3.10/Modules/FindPkgConfig.cmake:590 (if):
  if given arguments:

    "NOT" "DEFINED" "__pkg_config_checked_LIBGIT2" "OR" "__pkg_config_checked_LIBGIT2" "LESS" "OR" "NOT" "LIBGIT2_FOUND" "OR" "(" "NOT" "libgit2" "STREQUAL" "" "AND" "NOT" "" "STREQUAL" "REQUIRED;libgit2" ")" "OR" "(" "libgit2" "STREQUAL" "" "AND" "NOT" "" "STREQUAL" "REQUIRED" ")"

  Unknown arguments specified
Call Stack (most recent call first):
  src/CMakeLists.txt:5 (pkg_check_modules)

我不明白如何正确地链接和构建这个库到我的项目。

编辑2 我单独构建了 libgit2 库。 之后,我更改了主 CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(project_name)

add_subdirectory(src)
add_subdirectory(tests)

此外,我还更改了 src 文件夹中的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(project_name)

add_executable(project_namemain.cpp)

target_include_directories(project_name PUBLIC ../libgit2/include)
target_link_libraries(project_name ../libgit2/build/Debug/git2)

我得到了正常配置和生成的项目。 但是当我开始构建项目时,我收到错误:

fatal error LNK1104: cannot open file 'libgit2/build/Debug/git2.lib'

如何修复这个错误?我尝试了不同的方法来修复此错误,但没有成功。

最佳答案

为了使 CMake 正确确定链接器语言,您需要将至少一个源文件添加到 add_library不仅调用源目录。

添加源文件可以通过调用来完成

file(GLOB_RECURSE libgit2_src ../libgit2/src/*.cpp)

set(libgit2_src ../libgit2/src/file1.cpp ../libgit2/src/file2.cpp)

在您的 CMakeLists.txt 中,而后者是首选(原因请参阅 here )。

编辑:我有点被OP最初的解决方案误导了。我的印象是libgit2是他自己的图书馆。

您不应该尝试包含 libgit2源到您的项目中。 相反,构建 libgit2按以下顺序(您可能需要调整 OpenSSLZLib 库的路径或忽略它)在 <libgit2downloaddir> 中:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/install/prefix -DZLIB_LIBRARY=/installed/zlib -DOPENSSL_SSL_LIBRARY=/install/libssl.a -DOPENSSL_CRYPRO_LIBRARY=installed/libcrypto.a
make install

libgit2在您的项目中不提供包配置模块(git2-config.cmake 或 git2Config.cmake),您需要执行

target_include_directories(younameit <pathtoinstalledlibgit2includes>)
target_link_library(younameit <fullpathto>/libgit2.a)

关于c++ - 在cmake项目中构建libgit2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49892597/

相关文章:

c++ - 如何确定安装了哪个版本的 Direct3D?

c++ - 未找到相关库的 Makefile

c++ - std::result_of 用于内置运算符

c - 让 gcc 发出有关 "jump to label crosses initialization of variable"的警告

Windows 上的 git shell 报告 "sh.exe has stopped working (APPCRASH)"

c++ - 比较栈数据结构c++

c - 某些输入出现段错误,其他输入则不出现段错误

c - 是否可以抑制 Xcode (clang) 分析器报告的问题实例?

git pull 错误

git - 如何在本地和远程删除 Git 分支?