cmake find_package(X [version]) always finds highest version of X if multiple versions installed

I’m running cmake 3.23.0. I want to find_library(VTK) based on version criteria. My ubuntu 20.04 system has two installed versions of VTK:

/usr/local/lib/cmake/vtk-8.2
/usr/local/lib/cmake/vtk-9.0

My project requires VTK version 8.x.x, i.e. need to exclude VTK 9.x.x - so CMakeLists.txt includes:

   find_package(VTK "8...<9" REQUIRED)

But VTK_VERSION found is 9.0.1 - why, given the constraint? Also tried

find_package(VTK "8.0...<9.0" REQUIRED)
find_package(VTK "8.0.0...<9.0.0" REQUIRED)

with the same result - VTK_VERSION 9.0.1 is found. Tried without a version range, but still find_package() returns VTK_VERSION 9.0.1:

   find_package(VTK "8" REQUIRED) 
   find_package(VTK “8.2” REQUIRED)
   find_package(VTK “8.2.0” REQUIRED)

The only thing that actually returns VTK_VERSION 8.2.0:

   find_package(VTK “8.2.0” REQUIRED EXACT)

Is this behavior due to the fact that two VTK versions are installed on my system, and find_packages() assumes just one is installed?

Version range is not supported by all modules. This probably explains why specifying a range seems ignored.

Moreover, without the EXACT keyword, the standard meaning, when a version is specified, is to search for the most recent version which is greater or equal to this version.

So what you describe is the expected behavior…

2 Likes

Thanks - so when I’m building a new project which needs third-party packages, I can’t count on version-range support? Makes the feature not-so-useful :wink: So how to determine whether it’s supported for a given package?

You can’t in general. VTK could certainly be updated to handle this though. Could you please file an issue?

So @ben.boeckel from my description, it’s safe to say that version-range is either not implemented or is broken for at VTK-8.2 (rather than a cmake problem)? Sure I can file an issue!

Well, 8.2 isn’t getting fixed (nor 9.1 for that matter), but future VTK versions can learn new tricks at least

Yeah, that’s what I and many other users are expecting.

FWIW, VTK does have a vtk-config-version.cmake that should be doing this. Could you trace that code to see why it thinks ...8.2 is suitable for it?