簡體   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