1

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.

igdev
  • 341
  • 1
  • 4
  • 15
  • 1
    `How correctly to build and include library libgit2 in my project?` - Why do not read the project's [README](https://github.com/libgit2/libgit2/blob/master/README.md#building-libgit2---using-cmake) and follow it when building `libgit2`? As for using the package, link with installed `git2` library and include corresponded directories. Or, because `libgit2` provides `.pc` file, use `pkg_check_modules`, like [my answer](https://stackoverflow.com/a/35476270/3440745) describes. – Tsyvarev Apr 18 '18 at 08:02
  • According to your last edits. Please try using absolute path names for libraries and include directories first. Later you can try to find out how to solve using other means. Second, does a library named git2.lib exists in the Debug folder? And if yes, when adding a `message(STATUS "${CMAKE_CURRENT_BINARY_DIR}")` can its output + relative path to the library be resolved? – vre Apr 20 '18 at 21:48
  • @vre Thank you! :) The problem was that path to the library was incorrect. – igdev Apr 21 '18 at 08:46

1 Answers1

1

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)
vre
  • 6,041
  • 1
  • 25
  • 39
  • I've made first way because in src folder are located many *.c files. This method removed the error about which I wrote above, but emerged were many new mistakes. For example: `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 ` P.S. libgit2 is an opensource library. Her code can look [here](https://github.com/libgit2/libgit2). I hope this can help resolve my problem. – igdev Apr 18 '18 at 07:31
  • 1
    Header `git2/sys/features.h` is generated when `libgit2` is configured by its `CMakeLists.txt`. Building the project from the **plain sources** isn't a wise choose. Instead, use CMake for build it, as described in its README. – Tsyvarev Apr 18 '18 at 08:08
  • @Tsyvarev Ah, now I see. I was mislead by the OPs first solution. – vre Apr 18 '18 at 08:20
  • @vre I've updated information about the problem. Please take a look. – igdev Apr 18 '18 at 15:58
  • @vre Today I've fixed some problems, but emerged a new problem. Please take a look. – igdev Apr 20 '18 at 20:08