【问题标题】:Build libgit2 in cmake project在 cmake 项目中构建 libgit2
【发布时间】:2018-04-18 06:26:21
【问题描述】:

我有下一个项目树:

>googletest
>libgit2
>src
>tests

main 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)

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

EDIT2 我已经单独构建了库 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'

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

【问题讨论】:

  • How correctly to build and include library libgit2 in my project? - 为什么不阅读项目的README 并在构建libgit2 时关注它?至于使用包,链接安装的git2库并包含对应的目录。或者,因为libgit2 提供了.pc 文件,所以使用pkg_check_modules,就像my answer 描述的那样。
  • 根据您上次的编辑。请尝试使用库的绝对路径名并首先包含目录。稍后您可以尝试找出如何使用其他方式解决。其次,Debug文件夹中是否存在名为git2.lib的库?如果是的话,当添加message(STATUS "${CMAKE_CURRENT_BINARY_DIR}") 时,它的输出+ 到库的相对路径可以解决吗?
  • @vre 谢谢! :) 问题是库的路径不正确。

标签: c++ c git cmake libgit2


【解决方案1】:

要让 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 源包含到您的项目中。 而是在<libgit2downloaddir> 中按以下顺序构建libgit2(您可能需要将路径调整为OpenSSLZLib 库或省略它):

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)

【讨论】:

  • 我采用了第一种方式,因为在 src 文件夹中有许多 *.c 文件。这种方法消除了我上面写的错误,但出现了许多新的错误。例如:Error C1083 Cannot open include file: 'git2/sys/features.h': No such file or directory Error C1083 Cannot open include file: 'common.h': No such file or directory libgit2 是一个开源库。她的代码可以看here。我希望这可以帮助解决我的问题。
  • 标头git2/sys/features.h是在libgit2由其CMakeLists.txt配置时生成的。从普通来源构建项目并不是一个明智的选择。相反,请使用 CMake 来构建它,如其 README 中所述。
  • @Tsyvarev 啊,现在我明白了。我被 OP 的第一个解决方案误导了。
  • @vre 我已经更新了有关该问题的信息。请看一看。
  • @vre 今天我解决了一些问题,但又出现了一个新问题。请看一看。
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多