openexr: Windows: Can't link a project to OpenEXR that was built with static zlib

Branch: master (f04fcdd52d9de6ec8e17a2938e1e7b46428aad4a)

Trying to use a Windows build of OpenEXR built with built-in zlib fails at first with the following error at CMake time:

Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
...
... /openexr/lib/cmake/OpenEXR/OpenEXRConfig.cmake:31 (find_dependency)

This is due to find_dependency(ZLIB REQUIRED) in OpenEXRConfig.cmake:31. If I manually comment out that line, CMake succeeds, but now I get a linker error:

LINK : fatal error LNK1181: cannot open input file 'zlib_static.lib'

This is due to zlib_static being present in the INTERFACE_LINK_LIBRARIES of OpenEXR::OpenEXR If I also manually remove zlib_static (from OpenEXRTargets.cmake) then my program all builds and runs fine.

I believe if OpenEXR is built with internal static zlib library, then zlib should not appear as a dependency and neither at the interface link libraries.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 3
  • Comments: 16 (13 by maintainers)

Most upvoted comments

We ran into the same issue while using OpenEXR as a third-party library for OpenCV. It seems that OpenEXR’s dependency to zlib adds a reference to zlib_static.lib. Here, not only the underscore is wrong (i.e. zlibstatic.lib is correct) but also the path is missing. We think that this has to be fixed during the generation of OpenEXRTargets.cmake inside lib\cmake\OpenEXR of the respective installation directory.

It made no difference (should it have?, did it work for you?), resulting OpenEXRConfig.cmake and OpenEXRTargets.cmake ended up being the same. However using this change did:

diff --git a/src/lib/OpenEXR/CMakeLists.txt b/src/lib/OpenEXR/CMakeLists.txt
index 64797f67..0775daac 100644
--- a/src/lib/OpenEXR/CMakeLists.txt
+++ b/src/lib/OpenEXR/CMakeLists.txt
@@ -189,5 +189,6 @@ openexr_define_library(OpenEXR
     OpenEXR::Iex
     OpenEXR::IlmThread
     Imath::Imath
+  PRIVATE_DEPS
     ZLIB::ZLIB
   )

But it only removed the zlib_static from the INTERFACE_LINK_LIBRARIES of the OpenEXR::OpenEXR target in OpenEXRTargets.cmake. The find_dependency(ZLIB REQUIRED) still remained in OpenEXRConfig.cmake, and that also needs to be removed. Unfortunately this change is not suitable, as it must only be used as a PRIVATE_DEPS if zlib was built internally, not otherwise (I think?).

EDIT: BTW, I don’t get this error at all:

CMake Error at cmake/OpenEXRSetup.cmake:241 (add_library):
  add_library cannot create ALIAS target "ZLIB::ZLIB" because target
  "zlib_static" is imported but not globally visible.

OpenEXR builds fine as it is, it is only when I try to use it that I have the issues.

CMake version I use is 3.18.1.