LNK2005 error when use VTK&OpenCV static library in VS2019

Conflict between vtkpng-9.0.lib and libpng.lib, but all of them must be link.
VTK use vtkpng-9.0.lib and vtkfreetype-9.0.lib,and opencv must link libpng.lib and freetype.lib.
I don’t know how to fix it.
How to build VTK static library can use extenal libpng and freetype static library and don’t have to link vtkpng-9.0.lib and vtkfreetype-9.0.lib again?

Build started...
1>------ Build started: Project: QtImageProcessing, Configuration: Release x64 ------
1>Reading Qt configuration (D:/Qt/5.15.2/msvc2019_64/bin/qmake)
1>libpng.lib(pngget.obj) : error LNK2005: png_get_eXIf already defined in vtkpng-9.0.lib(pngget.obj)
1>libpng.lib(pngget.obj) : error LNK2005: png_get_eXIf_1 already defined in vtkpng-9.0.lib(pngget.obj)
1>libpng.lib(pngset.obj) : error LNK2005: png_set_eXIf already defined in vtkpng-9.0.lib(pngset.obj)
1>libpng.lib(pngset.obj) : error LNK2005: png_set_eXIf_1 already defined in vtkpng-9.0.lib(pngset.obj)
1>libpng.lib(pngrutil.obj) : error LNK2005: png_check_chunk_length already defined in vtkpng-9.0.lib(pngrutil.obj)
1>libpng.lib(pngrutil.obj) : error LNK2005: png_handle_eXIf already defined in vtkpng-9.0.lib(pngrutil.obj)
1>libpng.lib(pngwutil.obj) : error LNK2005: png_write_eXIf already defined in vtkpng-9.0.lib(pngwutil.obj)
1>vtkfreetype-9.0.lib(ftbase.obj) : error LNK2005: FT_MulFix already defined in freetype.lib(ftbase.c.obj)
1>vtkfreetype-9.0.lib(psaux.obj) : error LNK2005: cff_builder_funcs already defined in freetype.lib(psaux.c.obj)
1>vtkfreetype-9.0.lib(psaux.obj) : error LNK2005: ps_builder_funcs already defined in freetype.lib(psaux.c.obj)
1>vtkfreetype-9.0.lib(psaux.obj) : error LNK2005: cff_decoder_funcs already defined in freetype.lib(psaux.c.obj)

Sorry, I can’t find a solution quickly until I finish my help post.
I found a solution at c++ - How to solve libpng function already defined error? - Stack Overflow

1 Like

It seems like PNG is exporting some new functions that we need to mangle. Alternatively, you can have VTK build against the same external PNG and Freetype libraries you’re using elsewhere in your software stack.

I’ve reported an issue.