Adding a package version to my library

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?

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:

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
# ...

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

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

Then this is created:

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

${PROJECT_NAME}TargetsConfigVersion.cmake

Hm, it could be that you are “violating” the file name convention here. The documentation 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 (and then also at install() here).

…Also I couldn’t find where you define the EbsdLib_VERSION that is used here, but if the resulting version config gets the right value, the apparently you do in fact have it defined somewhere.