# Find package without myLib.cmake

**URL:** <https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420>\
**Category:** Code\
**Tags:** os:macos\
**Created:** [April 10, 2022, 12:22pm UTC](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420 "2022-04-10T12:22:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mat\_55](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mat\_55](https://discourse.cmake.org/u/Mat_55)\
**Post date:** [April 10, 2022, 12:22pm UTC](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420/1 "2022-04-10T12:22:06Z")

</div>

Hi,

I am trying to use [libgit2](https://github.com/libgit2/libgit2) in my C++ project. I installed the libgit2 library through brew (macOS).

The CMake configuration of my project fails with the following message:

```auto
  CMake Warning at CMakeLists.txt:6 (find_package):
  By not providing "Findlibgit2.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "libgit2", but
  CMake did not find one.

  Could not find a package configuration file provided by "libgit2" with any
  of the following names:

    libgit2Config.cmake
    libgit2-config.cmake

  Add the installation prefix of "libgit2" to CMAKE_PREFIX_PATH or set
  "libgit2_DIR" to a directory containing one of the above files. If
  "libgit2" provides a separate development package or SDK, be sure it has
  been installed.

```

I checked the content of the directory where the library is installed (/opt/homebrew/Cellar/libgit2/1.4.2\_1) and indeed there is no \*.cmake package configuration:

```auto
.
├── AUTHORS
├── COPYING
├── INSTALL_RECEIPT.json
├── README.md
├── include
│ ├── git2
│ └── git2.h
├── lib
│ ├── libgit2.1.4.0.dylib
│ ├── libgit2.1.4.dylib -> libgit2.1.4.0.dylib
│ ├── libgit2.a
│ ├── libgit2.dylib -> libgit2.1.4.dylib
│ └── pkgconfig
└── share
    └── libgit2

```

As there is no package configuration but the library exists, what is the proper way to include it in my project?

If it helps, here is a minimal example:

```auto
cmake_minimum_required(VERSION 3.21)

project(libgit2_example CXX)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/opt/homebrew/Cellar/libgit2/1.4.2_1")
find_package(libgit2)

add_executable(${CMAKE_PROJECT_NAME})

target_sources(${CMAKE_PROJECT_NAME}
        PRIVATE
        main.cpp)

target_include_directories(${CMAKE_PROJECT_NAME}
        PRIVATE
        /opt/homebrew/Cellar/libgit2/1.4.2_1/include)

target_link_libraries(${CMAKE_PROJECT_NAME}
        PRIVATE
        libgit2)

```

```auto
#include "git2.h"

int main() {
    git_libgit2_init();
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 11, 2022, 5:48pm UTC](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420/2 "2022-04-11T17:48:00Z")

</div>

> [@Mat\_55](#):
>
> As there is no package configuration but the library exists, what is the proper way to include it in my project?

`libgit2` provides a `.pc` file (under `lib/pkgconfig` I assume). You can use [`FindPkgConfig`](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html) and its APIs to make that information available in CMake.

---

<div class="post-metadata">

**Author:** ![Mat\_55](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a9adbd/32.png) [@Mat\_55](https://discourse.cmake.org/u/Mat_55)\
**Post date:** [April 12, 2022, 8:05am UTC](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420/3 "2022-04-12T08:05:28Z")

</div>

From what I understood from FindPkgConfig, I came to this solution:

```cmake
cmake_minimum_required(VERSION 3.21)

project(libgit2_example CXX)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/opt/homebrew/Cellar/libgit2")
find_package(PkgConfig)
if (PkgConfig_FOUND)
    pkg_check_modules(LIBGIT2 libgit2)
endif ()

add_executable(${CMAKE_PROJECT_NAME})

target_sources(${CMAKE_PROJECT_NAME}
        PRIVATE
        main.cpp)

target_include_directories(${CMAKE_PROJECT_NAME}
        PRIVATE
        ${LIBGIT2_INCLUDE_DIRS})

target_link_directories(${CMAKE_PROJECT_NAME}
        PRIVATE
        ${LIBGIT2_LIBRARY_DIRS})

target_link_libraries(${CMAKE_PROJECT_NAME}
        PRIVATE
        ${LIBGIT2_LIBRARIES})

```

It works. Thanks.
