# Adding a package version to my library

**URL:** <https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725>\
**Category:** Code\
**Created:** [September 19, 2024, 2:36pm UTC](https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725 "2024-09-19T14:36:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imikejackson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/imikejackson/32/301_2.png) [@imikejackson](https://discourse.cmake.org/u/imikejackson)\
**Post date:** [September 19, 2024, 2:36pm UTC](https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725/1 "2024-09-19T14:36:39Z")

</div>

I have a library that I have created using C++ and CMake: EbsdLib. I have everything working well and everything installs but I have noticed that I cannot retrieve the current version of the library through the normal CMake process. I feel like I need to have something custom included in my write\_basic\_package\_version\_file() call? I was wanting something like “EbsdLib\_VERSION” that would get set from the EbsdLibConfig.cmake file that gets generated? Or am I just missing something? I tried just printing through message(STATUS “EbsdLib\_VERSION: ${EbsdLib\_VERSION}”) just after “find\_package(EbsdLib REQUIRED)” in the project that is consuming the EbsdLib but nothing printed.

Thoughts?

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 19, 2024, 3:00pm UTC](https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725/2 "2024-09-19T15:00:54Z")

</div>

Do you actually set the project version in your library’s `CMakeLists.txt`?  
And then on your project installation, do you get the `*ConfigVersion.cmake` (_or how did you call it_) file? What does it contain?

> I feel like I need to have something custom included in my write\_basic\_package\_version\_file() call

Here’s what I usually put into mine:

```auto
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
    COMPATIBILITY AnyNewerVersion
)
# don't forget that it also needs to be installed
# ...

```

---

<div class="post-metadata">

**Author:** ![imikejackson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/imikejackson/32/301_2.png) [@imikejackson](https://discourse.cmake.org/u/imikejackson)\
**Post date:** [September 19, 2024, 3:49pm UTC](https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725/3 "2024-09-19T15:49:22Z")

</div>

Yes, we do set the version in the project call:

> <https://github.com/BlueQuartzSoftware/EbsdLib/blob/d280acdd2f5d1c6e9df3b806f692487a4c8b87b7/CMakeLists.txt#L18>

Here is the CMake code for the various package installed files:

> <https://github.com/BlueQuartzSoftware/EbsdLib/blob/d280acdd2f5d1c6e9df3b806f692487a4c8b87b7/Source/EbsdLib/SourceList.cmake#L249>

Then this is created:

```auto
set(PACKAGE_VERSION "1.0.33")

if (PACKAGE_FIND_VERSION_RANGE)
  # Package version must be in the requested version range
  if ((PACKAGE_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION_MIN)
      OR ((PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND PACKAGE_VERSION VERSION_GREATER PACKAGE_FIND_VERSION_MAX)
        OR (PACKAGE_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND PACKAGE_VERSION VERSION_GREATER_EQUAL PACKAGE_FIND_VERSION_MAX)))
    set(PACKAGE_VERSION_COMPATIBLE FALSE)
  else()
    set(PACKAGE_VERSION_COMPATIBLE TRUE)
  endif()
else()
  if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
    set(PACKAGE_VERSION_COMPATIBLE FALSE)
  else()
    set(PACKAGE_VERSION_COMPATIBLE TRUE)
    if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
      set(PACKAGE_VERSION_EXACT TRUE)
    endif()
  endif()
endif()

# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "8" STREQUAL "")
  return()
endif()

# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "8")
  math(EXPR installedBits "8 * 8")
  set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
  set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()

```

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [September 19, 2024, 4:45pm UTC](https://discourse.cmake.org/t/adding-a-package-version-to-my-library/11725/4 "2024-09-19T16:45:18Z")

</div>

> ${PROJECT\_NAME}TargetsConfigVersion.cmake

Hm, it could be that you are “violating” the file name convention here. The [documentation](https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-version-selection) says:

> the corresponding version file is located next to it and named either `<config-file>-version.cmake` or `<config-file>Version.cmake`

So I would try dropping the `Targets` part of your version config file name [here](https://github.com/BlueQuartzSoftware/EbsdLib/blob/d280acdd2f5d1c6e9df3b806f692487a4c8b87b7/Source/EbsdLib/SourceList.cmake#L252) (_and then also at `install()` [here](https://github.com/BlueQuartzSoftware/EbsdLib/blob/d280acdd2f5d1c6e9df3b806f692487a4c8b87b7/Source/EbsdLib/SourceList.cmake#L279)_).

…Also I couldn’t find where you define the `EbsdLib_VERSION` that is used [here](https://github.com/BlueQuartzSoftware/EbsdLib/blob/d280acdd2f5d1c6e9df3b806f692487a4c8b87b7/Source/EbsdLib/SourceList.cmake#L253), but if the resulting version config gets the right value, the apparently you do in fact have it defined somewhere.
