# vcpkg Problems

**URL:** <https://discourse.itk.org/t/vcpkg-problems/3151>\
**Category:** Engineering\
**Tags:** vcpkg\
**Created:** [June 5, 2020, 6:38pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151 "2020-06-05T18:38:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 5, 2020, 6:38pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/1 "2020-06-05T18:38:29Z")

</div>

Hello,

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:

```auto
ITK_LIBRARIES="ITKLabelMap;ITKCommon;ITKStatistics;ITKTransform;ITKSpatialObjects;ITKPath;Eigen3::Eigen;itksys;itkvnl_algo;itkvnl;itkv3p_netlib;itknetlib;itkvcl;itkNetlibSlatec;ITKMetaIO;/lib/libz.a;ITKIOImageBase;ITKIOTransformBase;ITKTransformFactory;ITKMesh;ITKIOBMP;ITKIOBioRad;ITKIOBruker;ITKIOGDCM;gdcmDICT;gdcmMSFF;/lib/libexpat.a;ITKIOGE;ITKIOIPL;ITKIOGIPL;ITKIOHDF5;ITKIOJPEG;ITKIOJPEG2000;ITKIOLSM;ITKIOTIFF;ITKIOMINC;ITKIOMRC;ITKIOMeta;ITKIONIFTI;ITKIONRRD;ITKIOPNG;ITKIOStimulate;ITKIOVTK;ITKIOTransformHDF5;ITKIOTransformInsightLegacy;ITKIOTransformMatlab"

```

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

```auto
[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:

```auto
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:

```auto
find_package(ITK 5.0.0 REQUIRED
              COMPONENTS
                ITKBinaryMathematicalMorphology
                ITKCommon
                ITKConnectedComponents
                ITKFFT
                ITKIOImageBase
                ITKIOTransformBase
                ITKImageCompose
                ITKImageFeature
                ITKImageFilterBase
                ITKImageGrid
                ITKImageIntensity
                ITKImageStatistics
                ITKLabelMap
                ITKLabelVoting
                ITKMathematicalMorphology
                ITKThresholding
                ITKTransform
                ITKImageIO
                ITKTransformIO
                ITKIONIFTI )
include( ${ITK_USE_FILE} )

```

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 5, 2020, 7:28pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/2 "2020-06-05T19:28:28Z")

</div>

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: [https://github.com/microsoft/vcpkg/issues/6975](https://github.com/microsoft/vcpkg/issues/6975) 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…

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 5, 2020, 8:15pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/3 "2020-06-05T20:15:51Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 5, 2020, 9:33pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/4 "2020-06-05T21:33:06Z")

</div>

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:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/51cfc2926fe0cf860e2843e0ed51ee2363e20543/CMake/ITKConfig.cmake.in#L11-L15>

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.

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 6, 2020, 7:32pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/5 "2020-06-06T19:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [June 6, 2020, 10:41pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/6 "2020-06-06T22:41:40Z")

</div>

> [@spinicist](#):
>
> `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)`

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.

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 8, 2020, 7:49am UTC](https://discourse.itk.org/t/vcpkg-problems/3151/7 "2020-06-08T07:49:16Z")

</div>

Oh!

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?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [June 8, 2020, 2:56pm UTC](https://discourse.itk.org/t/vcpkg-problems/3151/8 "2020-06-08T14:56:35Z")

</div>

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](https://github.com/InsightSoftwareConsortium/ITK/issues/1807#issuecomment-625526368) is related to OpenJPEG. If you can translate his patch into an ITK PR it would be good.

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [June 18, 2020, 8:59am UTC](https://discourse.itk.org/t/vcpkg-problems/3151/9 "2020-06-18T08:59:44Z")

</div>

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