Menu

#246 Linker errors for Simplified API (libpng 1.6.21)

libpng_code
closed-invalid
5
2016-04-12
2016-04-12
Andrei
No

I am using MinGW/MSYS on Windows XP.

I cannot build the example program at contrib/examples/pngtopng.c because of linker errors:

$ gcc --version
gcc.exe (tdm64-1) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ pwd
/home/A/libpng-1.6.21/contrib/examples
$ md5sum pngtopng.c
6543e180dd99dc4832b5e0386d68e995 *pngtopng.c
$ gcc -m32 pngtopng.c -o pngtopng.exe
C:\DOCUME~1\A\LOCALS~1\Temp\ccc91WIy.o:pngtopng.c:(.text+0x5e): undefined reference to `png_image_begin_read_from_file'
C:\DOCUME~1\A\LOCALS~1\Temp\ccc91WIy.o:pngtopng.c:(.text+0x106): undefined reference to `png_image_finish_read'
C:\DOCUME~1\A\LOCALS~1\Temp\ccc91WIy.o:pngtopng.c:(.text+0x149): undefined reference to `png_image_write_to_file'
C:\DOCUME~1\A\LOCALS~1\Temp\ccc91WIy.o:pngtopng.c:(.text+0x1da): undefined reference to `png_image_free'
collect2.exe: error: ld returned 1 exit status

In advance of building libpng, the zlib 1.2.8 library was successfully built and installed.

The PNG library was successfully built and installed with the following commands:

$ cd libpng-1.6.21/
$ ./configure \
> CFLAGS="-march=native -mtune=native -Ofast -m32" \
> CPPFLAGS="-I/local/include" \
> LDFLAGS="-L/local/lib"
$ make
$ make install-strip

My Questions, ordered by decreasing importance:

  1. How to fix the linker errors above, and successfully use the Simplified API?
  2. How to build a .DLL instead of a statically linked library?

Discussion

  • Glenn Randers-Pehrson

    Your build command for pngtopng.exe needs to include appropriate -I and -L options so gcc can find libpng, and "-lpng -lz" options as well.

     
  • Andrei

    Andrei - 2016-04-12

    Adding the above options does not work, as shown below.

    I believe the problem has to do with the Simplified API being skipped for compilation due to bad configuration or bad macros. I wish to see your own commands for building the library and the pngtopng program.

    $ find /local/lib/*png*
    /local/lib/libpng.a
    /local/lib/libpng.la
    /local/lib/libpng16.a
    /local/lib/libpng16.la
    $ gcc -m32 -I/local/include -L/local/lib -lpng -lz -o pngtopng.exe pngtopng.c
    C:\DOCUME~1\A\LOCALS~1\Temp\cc84oykZ.o:pngtopng.c:(.text+0x5e): undefined reference to `png_image_begin_read_from_file'
    C:\DOCUME~1\A\LOCALS~1\Temp\cc84oykZ.o:pngtopng.c:(.text+0x106): undefined reference to `png_image_finish_read'
    C:\DOCUME~1\A\LOCALS~1\Temp\cc84oykZ.o:pngtopng.c:(.text+0x149): undefined reference to `png_image_write_to_file'
    C:\DOCUME~1\A\LOCALS~1\Temp\cc84oykZ.o:pngtopng.c:(.text+0x1da): undefined reference to `png_image_free'
    collect2.exe: error: ld returned 1 exit status
    
     
  • Glenn Randers-Pehrson

    glenn.rp> ./configure --prefix=$HOME
    glenn.rp> make
    glenn.rp> make install
    glenn.rp> cd contrib/examples
    glenn.rp> gcc -L/$HOME/lib -I/$HOME/include -o pngtopng pngtopng.c -lpng16 -lz
    glenn.rp> ./pngtopng ../../pngtest.png pngout.png
    glenn.rp> ls -l pngout.png
    -rw-rw-r-- 1 glennrp glennrp 6504 Apr 12 17:46 pngout.png
    glenn.rp> ldd pngtopng
    linux-vdso.so.1 => (0x00007fff29bf9000)
    libpng16.so.16 => /home/glennrp/lib/libpng16.so.16 (0x00007fc133fec000)
    libc.so.6 => /lib/x8664-linux-gnu/libc.so.6 (0x00007fc133c27000)
    libz.so.1 => /home/glennrp/lib/libz.so.1 (0x00007fc133a10000)
    libm.so.6 => /lib/x8664-linux-gnu/libm.so.6 (0x00007fc13370a000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fc13421e000)

    Note that the "-lpng16 -lz" (or "-lpng -lz") have to come at the end of the gcc command, or at any rate, after "pngtopng.c".

     

    Last edit: Glenn Randers-Pehrson 2016-04-12
  • Andrei

    Andrei - 2016-04-12

    Note that the "-lpng16 -lz" (or "-lpng -lz") have to come at the end of the gcc command, or at any rate, after "pngtopng.c".

    Thank you for your assistance.
    Indeed the problem was caused by the bad placement of -lpng in the build command.

    This behavior is deserving of at least a warning message from GCC, but that's off-topic.

     
  • Glenn Randers-Pehrson

    • status: open --> closed-invalid
     

Log in to post a comment.

MongoDB Logo MongoDB