libheif icon indicating copy to clipboard operation
libheif copied to clipboard

Static linking on Windows/MSVC/vcpkg?

Open ziriax opened this issue 4 years ago • 3 comments

I'm experimenting with static linking and vcpkg, and I get a lot of undefined linker errors when linking the libraries into my final DLL; it seems libheif is trying to reference libde265 dynamically even when building statically, like:

Creating library Release\eXpress.Imaging.Heif.Native.lib and object Release\eXpress.Imaging.Heif.Native.exp
heif.lib(heif_decoder_libde265.cc.obj) : error LNK2019: unresolved external symbol __imp_de265_get_version referenced in function "char const * __cdecl libde265_plugin_name(void)" (?libde265_plugin_name@@YAPEBDXZ)

I was able to fix this by modifying the libheif/CMakeLists.txt as follows:

--- a/libheif/CMakeLists.txt
+++ b/libheif/CMakeLists.txt
@@ -54,10 +54,17 @@ set_target_properties(heif
                           VERSION ${PROJECT_VERSION}
                           SOVERSION ${PROJECT_VERSION_MAJOR})

-target_compile_definitions(heif
-                           PUBLIC
-                               LIBHEIF_EXPORTS
-                               HAVE_VISIBILITY)
+if(BUILD_SHARED_LIBS)
+    target_compile_definitions(heif
+                            PUBLIC
+                                LIBHEIF_EXPORTS
+                                HAVE_VISIBILITY)
+else()
+    target_compile_definitions(heif
+                               PUBLIC
+                                   LIBHEIF_STATIC_BUILD
+                                   LIBDE265_STATIC_BUILD)
+endif()

 if(LIBDE265_FOUND)
     target_compile_definitions(heif PRIVATE HAVE_LIBDE265=1)

I'm not sure this is the correct way to do this.

And maybe the libde265 lib should be modified accordingly?

ziriax avatar May 18 '21 12:05 ziriax

I just hit this as well while building a static version of libheif using LLVM on Windows.

kasperisager avatar Jan 28 '25 10:01 kasperisager

Same. OP's fix works for me, so for the time being my project depends on a fork of libheif with the fix implemented. (https://github.com/Tom94/tev/pull/253)

Tom94 avatar Feb 22 '25 15:02 Tom94

It is 2025, and the problem occurs again in vcpkg.

dg0yt avatar Jul 11 '25 18:07 dg0yt