vcpkg Problems


I’ve just tried to move my main project over to vcpkg. I’ve had two problems.

First is that the $ITK_LIBRARIES variable appears to be set incorrectly. It ends up being:


note that it includes /lib/libz.a and /lib/libexpat.a, which then leads to errors like this during building:

[build] ninja: error: '/lib/libz.a', needed by 'Source/qi', missing and no known rule to make it

If I manually set the ITK_LIBRARIES variable without those entries, and my code built. But then I hit a link problem:

ld: library not found for -lopenjp2

Looking at the linker line generated for my project, it seems that all the -L/library/search/paths/ are missing. The other libraries that vcpkg is responsible for are specified as absolute paths (i.e. the path to the .a is given), and so do not need the search paths.

Can anyone with more vcpkg experience provide some help? Thanks.

I am including ITK in CMakeLists.txt like this:

find_package(ITK 5.0.0 REQUIRED
                ITKIONIFTI )
include( ${ITK_USE_FILE} )

It turns out both my problems were due to using include( ${ITK_USE_FILE} ) and specifying ${ITK_LIBRARIES} for target_link_libraries. According to this issue: the correct way to use ITK with vcpkg is to specify the targets individually. The question now, of course, is to work out exactly which targets I need to specify. I’ve managed to get it to link, but I can’t open any images because my code claims no IO factories are registered…

(Apologies for the live rubber-duck debugging)

I’ve struggled with this for a bit longer and have reached the following conclusions:

  1. I should be using include(${ITK_USE_FILE}). That’s how the ITK factories are registered.
  2. The resulting ${ITK_LIBRARIES} are incorrect and should not be used.
  3. OpenJPEG does appear to have a broken vcpkg configuration. However, I think this is being pulled in by GDCM. But GDCM is included in the default factory list specified by ${ITK_USE_FILE}, which leads to a catch-22 situation where you must include GDCM in the list for target_link_libraries, but it causes linker errors.

I have solved this problem by editing ITKConfig.cmake. I moved line 81 to line 91 and changed it to:

itk_module_config(ITK ${ITK_MODULES_REQUESTED})

This means that instead of ${ITK_USE_FILE} pulling in all available IO factories, it only uses those that have been requested (in my case Nifti only). This removes GDCM (and all the others) from the list of required libraries, and I was finally able to proceed.

I think my edit to itk_module_config looks reasonable. I’m happy to open a PR, but I’m not actually sure where - against the ITK or vcpkg repo?

1 Like

Hello Tobias,

I currently use ( and expect the current behavior ) of having all the ImageIO modules registered when the ITKImageIO component is specified.

Looking at this comment:

It sounds as if you don’t use the “ITKImageIO” pseudo-module, you should not get all the ImageIO factories. You might want to dig around further with the code related to ITKImageIO and the magical factory registration.

Thanks Bradley.

I don’t think I explained myself clearly - it was a long evening. I think there are three interlinked problems:

  1. The OpenJPEG vcpkg entry is broken, and gives an incorrect CMake link command. Not sure if this is ITK’s problem :wink:
  2. The ${ITK_LIBRARIES} generated by the vcpkg version is also incorrect and cannot be used.
  3. itk_module_config currently pulls in all the modules that were compiled when ITK was built, and not the smaller set of modules requested via find_package(COMPONENTS)

Because the vcpkg version of ITK builds everything, I don’t think there is currently a way to use it and only get a subset of the file formats. My proposed change to the itk_module_config call would fix that.

Your find_package include ITKImageIO which is the component pulling in all the ImageIO and generating the factory registration mechanism. If you remove the package form the list these other ImageIO should not be automagically included in the CMake system.


Thanks again. My apologies, at some point while working on this, I had actually removed ITKImageIO from my list of components without noticing. You are correct, there is no change to itk_module_config required. If I only have ITKIONIFTI in my list of components I can build & link successfully.

Issues 1 & 2 above still remain, which means that at present you can’t pull in ITKImageIO with vcpkg. However, I suspect this is due to problems with other packages within vcpkg - so I guess I should open an issue on their repo instead?

If OpenJPEG is broken in vcpkg, you should open an issue there. Issue 2 is probably also due to vcpkg's mucking with include libraries.

Alexander from vcpkg team has been trying to get ITK to build cleanly, and he has encountered several issues (use GitHub’s filtering to see the list). This one is related to OpenJPEG. If you can translate his patch into an ITK PR it would be good.


Sorry for delay in response. I will try to open issues/PRs/etc. when I get time (which may not be soon!)