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.

10 Responses to “Compiling libpng in Visual Studio .NET {2005}”

  1. J SMith Says:

    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

  2. Dave Says:

    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.

  3. building libpng in visual studio 2008 « Bobobobo’s Weblog Says:

    […] you can download! see I am nice!), this may help you if you can’t get it to work […]

  4. bobobobo Says:

    yeah man, I just went through this hassle.

    next i’m trying DevIL http://openil.sourceforge.net/

  5. Lucas Says:

    “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

  6. Dudeson Says:

    Oh my god!
    I wanna marry you!
    Thank you so much bro!!!!
    IT WORKS!

  7. kowal Says:

    We tender internal and exterior Balustrades Gates, toing and slide with total spontaneous Fences, gates, grilles and other garden products: lamps, choicest, wells and bridges, curtain rods, fireplace accessories, chandeliers, sconces, chandeliers, mirrors. Metal furniture, dagger constructions on request. The make available includes a full vary of colors (at the sagacity of the customer) and installation. My comrades’s products are protected against corrosion (depict, zinc).
    Ironmongery
    Ironmongery interested me at the years of 8 years, kowalstwo when I looked at the line done by means of his father. My first, artistic wrought iron products already developed 2 years later. The smithy was spending every surplus gravity and so it is today, with the very passion as a remedy for blacksmithing morphed into regular work. My imagination allows me to contrive unique designs. On each of the products I augment a impassion of privatization, which means that they are primary and have unique wzory.Kowalstwo craft is our passion, career and hobbies.
    Ironmongery saga
    Since 1786 years, my descent is grown wrought, as there is averment of creation done about my ancestors. I am already the seventh period offspring tradition Kuplickich. All the knowledge and facility to engender owe my papa, who instilled in me blacksmithing. With a particular fondness doing works in metal, copper and rudeness on their own projects and assigned. All products are boost forged, giving them a unmatched demeanour and shape.

  8. mundUnsumszib Says:

    adult friend finder mobile app

    adult friend finder edmonton

  9. Cinaetent Says:

    Volnovaha blue flowers

  10. arthur Says:

    I had no prolbem with compiling libpng with included vs projects. I have used vs2008 express, with plain zlib an libpng, just untared and placed in respective folder. Zlib has to be renamed to “zlib” only. I think they used that naming to make project files independent of zlib verison.

Leave a Reply