简体   繁体   中英

Build libgit2 in cmake project

I've next project tree:

>googletest
>libgit2
>src
>tests

The code in main CMakeLists.txt:

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

The code CMakeLists.txt in src folder:

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)

When I attempting to generate the project, I'm getting next error:

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

How correctly to build and include library libgit2 in my project?

EDIT

In main CMakeLists.txt I've added next command:

cmake_minimum_required(VERSION 2.8)

project(project_name)

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

After, I've changed the code CMakeLists.txt in src folder:

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

But when I configuring the project, I'm getting the error:

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)

I do not understand how correctly to link and build this library to my project.

EDIT2 I've built library libgit2 separately. After, I've changed the main CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(project_name)

add_subdirectory(src)
add_subdirectory(tests)

Also, I've changed CMakeLists.txt in src folder:

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)

I'm getting the project that configures and generate normally. But when I starting build project I get the error:

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

How to fix this error? I've tried different ways fix this error, but nothing succeeded.

For CMake to correctly determine the linker language you need to add at least one source file to your add_library call not only the source directory.

Adding souce files can be done either by calling

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

or

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

in your CMakeLists.txt while the latter is preferred (for reasons see here ).

Edit : I was somewhat mislead by the OPs initial solution. I was under the impression libgit2 was his own library.

You should not try to include the libgit2 sources into your project. Instead build libgit2 by the following sequence (you might need to adapt the path to OpenSSL and ZLib libraries or omit that) in <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

As libgit2 does not provide package config modules (git2-config.cmake or git2Config.cmake) in your project you need to do a

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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