Compiling libpng in Visual Studio .NET {2005}
There is very spotty documentation on how to use libpng under Windows. I did find one site that talks about the subject, so you might want to refer to it as well.
1. Libpng Setup:
First, you’ll need to download the latest version of libpng and zlib. Extract them someplace (I placed them within my projects root, in a directory called ‘deps’). The pre-canned Visual Studio settings that come with libpng look for ‘zlib’ rather than ‘zlib-x.y.z’, so rename the directories so that they lack the version numbers.
2. Compiling libpng and zlib
Next go into <projectname>\deps\libpng\projects\visualc71 and open libpng.sln. Go to Build and run ‘Clean Solution’. Now you have a choice, you can either compile libpng as a dynamically loaded library, or a binary blob to statically link into your executable. You can compile either one as a debug or release build. Just make sure that whichever way you compile libpng (debug/release), is the same as what you compile your project. So choose an option, and go to Build -> Build Solution. This will compile both libpng and zlib.
3. Project Setup
Now you’ll need to set your project up to include the libpng and zlib files. Open your Visual Studio project, and go to Project -> <projectname> Properties. Expand Configuration Properties, and then expand C/C++. The top field on the right will read “Additional Include Directories”. Click it and add “C:\<projectname>\deps\zlib;C:\<projectname>\deps\libpng”.
Now close the C/C++ tree and expand the Linker tree, which is directly below it. Roughly 3/4 of the way down is a field called “Additional Library Directories”. Click it and add
“C:\<projectname>\deps\libpng\projects\visualc71\<projecttype>\ZLib;
C:\AnimTester\deps\libpng\projects\visualc71\<projecttype>”.
In my case, I compiled as Win32_LIB_Debug (Statically Linked, Debug Symbols included), so that’s what I set <projecttypeyoucompiledas> to.
Next open the Input node that is in the Linker tree. Under “Additional Dependancies” add libpngd.lib zlibd.lib if you chose to statically link, or libpng13d.lib zlib1d.lib if you compiled it as a dll. If you did compile as a dll, you will then need to copy the dll’s that were generated to the same directory as your executable, or to a directory loaded in PATH.
Now you can implement PNG reading or writing into your project.
4. Troubleshooting
Q: I get a ton of linking errors!
A: Visual Studio cannot find the lib files. Double check that they are compiled (libpng{..}.lib, zlib{…}.lib), that your projects Library dependancies includes the directories that they are in, and that the lib files are included in the Additional Dependancies field.
Q: Visual Studio spits out errors about undeclared identifiers!
A: Make sure you have #include <png.h> in your source. Also make sure that png.h’s path is included under Additional Included Directories in your projects settings.
November 8th, 2008 at 10:13 am
Followed your instructions and….
Error 1 error A2070: invalid instruction operands c:\Documents and Settings\YMode\My Documents\Visual Studio 2005\Projects\deps\zlib\contrib\masmx86\inffas32.asm 647
Error 2 error A2070: invalid instruction operands c:\Documents and Settings\YMode\My Documents\Visual Studio 2005\Projects\deps\zlib\contrib\masmx86\inffas32.asm 649
Error 3 error A2070: invalid instruction operands c:\Documents and Settings\YMode\My Documents\Visual Studio 2005\Projects\deps\zlib\contrib\masmx86\inffas32.asm 663
Error 4 error A2070: invalid instruction operands c:\Documents and Settings\YMode\My Documents\Visual Studio 2005\Projects\deps\zlib\contrib\masmx86\inffas32.asm 720
Error 5 error PRJ0019: A tool returned an error code from “Assembling…” zlib
Error 6 fatal error LNK1104: cannot open file ‘zlib1d.lib’ libpng
Error 11 fatal error LNK1104: cannot open file ‘Win32_DLL_ASM_Debug\libpng13d.lib’ pngtest
January 30th, 2009 at 10:40 am
I did it took and got a whack of syntax errors: “missing ‘{’ before ‘*’” and stuff like that. It whined about stuff like “charf” and “voidpf” and the like.
Geez authors of LIBPNG: What an pathetic mess you have laid. Why provide Visual C++ project files “ready to run” if the stupid thing doesn’t work. Did you even test it? I’ve cross-ported libraries across before - had no problems with LIBZIP or LIBJPEG, but LIBPNG is made of fail. Too many bugs. What a piece of crap. And get off the GNU C++ high horse too - no one uses that piece of crap. Visual C++ is what real people with real jobs use. Sheesh.
March 2nd, 2009 at 3:42 am
[…] you can download! see I am nice!), this may help you if you can’t get it to work […]
March 2nd, 2009 at 4:05 am
yeah man, I just went through this hassle.
next i’m trying DevIL http://openil.sourceforge.net/
October 3rd, 2009 at 12:09 am
“Followed your instructions and….
Error 1 error A2070: invalid instruction operands c:\Documents and Settings\YMode\My Documents\Visual Studio 2005\Projects\deps\zlib\contrib\masmx86\inffas32.asm 647″
Where has the answer
http://www.freeorion.org/index.php/Zlib