繁体   English   中英

在cmake项目中构建libgit2

[英]Build libgit2 in cmake project

我的下一个项目树:

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

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

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'

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

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

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

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

要么

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

在您的CMakeLists.txt中,而后者是首选(出于原因,请参见此处 )。

编辑 :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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM