简体   繁体   中英

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.

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 .

在此处输入图像描述

在此处输入图像描述

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).

在此处输入图像描述

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++ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM