Search code examples
c++visual-studionugetlibpng

How do I fix LNK2019 errors after adding libpng to a VS2019 C++ project through Nuget?


I am creating a solution that requires the use of libpng. I installed this library and its dependencies through Visual Studio 2019's Nuget Package Manager, and including <png.h> and using functions and macros from the library does not lead to errors. However, should I try building the solution, I get one LNK2019 error per every reference to the library function or variable. It seems to me that NuGet failed to add the library locations to the linker properties. I have found where are the .lib files located, but I'm lost as to which settings do I adjust so that the Studio looks for, and finds, these files in the NuGet directory.


Solution

  • The issue why the automatic nuget cannot add the lib dependencies into the Linker is that the c++ nuget is for v140 and v120 build tool. And if your project is created by VS2019, VS uses v142 build tool(for VS2019) by default.

    You can check the targets file(Methods to import lib automatically) from the nuget package <solution_folder>\packages\libpng.1.6.28.1\build\native\libpng.targets.

    enter image description here

    enter image description here

    So this file cannot find V140 and V120,(your VS2019 uses V142) make the condition to false, so that auto import lib fails forever. That is the reason.

    V140 is VC++2015 build tool while V120 is VC++ 2013 build tool.

    If your PC has installed VS2015 and VS2013, you can change build tool for VS0219 by right-click on the project Properties-->Configuration Properties-->General--> change Platfrom Toolset to Visual Studio 2015(v140).

    enter image description here

    For a better solution

    And if your PC only has VS2019 and also for a better workaround,

    You should install libpng-v142 nuget package instead and it is for VS2019 VC++.