Static linking on Windows/MSVC/vcpkg?
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?
I just hit this as well while building a static version of libheif using LLVM on Windows.
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)
It is 2025, and the problem occurs again in vcpkg.