12

I am trying to access png pixel data in my C code on. I found this library libpng. I downloaded latest version from this site, I am using Ubuntu 14.04. I followed the instructions in the INSTALL file. Everything went well. And then I tried to compile with gcc this piece of code. But I received this:

/tmp/ccWa9LDO.o: In function `read_png_file':
test.c:(.text+0x13c): undefined reference to `png_sig_cmp'
test.c:(.text+0x16f): undefined reference to `png_create_read_struct'
test.c:(.text+0x1a0): undefined reference to `png_create_info_struct'
test.c:(.text+0x1db): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x20c): undefined reference to `png_init_io'
test.c:(.text+0x220): undefined reference to `png_set_sig_bytes'
test.c:(.text+0x239): undefined reference to `png_read_info'
test.c:(.text+0x252): undefined reference to `png_get_image_width'
test.c:(.text+0x271): undefined reference to `png_get_image_height'
test.c:(.text+0x290): undefined reference to `png_get_color_type'
test.c:(.text+0x2af): undefined reference to `png_get_bit_depth'
test.c:(.text+0x2c4): undefined reference to `png_set_interlace_handling'
test.c:(.text+0x2e3): undefined reference to `png_read_update_info'
test.c:(.text+0x2fc): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x36f): undefined reference to `png_get_rowbytes'
test.c:(.text+0x3b2): undefined reference to `png_read_image'
/tmp/ccWa9LDO.o: In function `write_png_file':
test.c:(.text+0x430): undefined reference to `png_create_write_struct'
test.c:(.text+0x461): undefined reference to `png_create_info_struct'
test.c:(.text+0x49c): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x4cd): undefined reference to `png_init_io'
test.c:(.text+0x4e6): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x559): undefined reference to `png_set_IHDR'
test.c:(.text+0x572): undefined reference to `png_write_info'
test.c:(.text+0x58b): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x5bf): undefined reference to `png_write_image'
test.c:(.text+0x5d8): undefined reference to `png_set_longjmp_fn'
test.c:(.text+0x607): undefined reference to `png_write_end'
/tmp/ccWa9LDO.o: In function `process_file':
test.c:(.text+0x692): undefined reference to `png_get_color_type'
test.c:(.text+0x6be): undefined reference to `png_get_color_type'
test.c:(.text+0x6db): undefined reference to `png_get_color_type'
collect2: error: ld returned 1 exit status

I don't understand it because I would expect that if there is problem with installation I would get errors just for including png.h.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Michal Krakovsky
  • 795
  • 3
  • 7
  • 17

3 Answers3

18

You said in the comments that you use gcc my_code.c, try

gcc my_code.c -lpng

The -l flag links a library, in this case libpng12-dev.

Linking means that your compiler adds the code from all the object files to create a single executable file. The object files are the separate compiled source code files (the .o files).

tversteeg
  • 4,717
  • 10
  • 42
  • 77
  • thanks, this helped with the compilation. Now I have problem running the program. I get this error message: `./a.out: error while loading shared libraries: libpng16.so.16: cannot open shared object file: No such file or directory` – Michal Krakovsky Aug 28 '14 at 10:56
  • I think that numbers there represents only version of that library. When I try `gcc my_code.c -lpng12` I get few lines of these: `test.c:(.text+0x1db): undefined reference to \`png_set_longjmp_fn'`. I tried `gcc my_code.c -lpng16` as well and the error from comnent above still persists. – Michal Krakovsky Aug 28 '14 at 11:40
  • Yes it does indeed only solve the version issue, what if you compile libpng 1.6 from source and try it with the command I said in my answer? – tversteeg Aug 28 '14 at 11:56
  • 1
    I compiled it from source I guess, I did it trough make install, not apt-get install. – Michal Krakovsky Aug 28 '14 at 12:35
  • You can use `find /usr/lib -name *png*` to find the location and then use `gcc my_code.c -L -lpng16`. – tversteeg Aug 28 '14 at 13:11
  • But I have no longer problem with compilation but with running the compiled program. I tried now compiling like this: `gcc test.c -L/usr/lib/syslinux/com32/include/ -lpng16`, but the problem still persists. – Michal Krakovsky Aug 28 '14 at 13:21
  • I wouldn't know the solution to this problem, I think you should open a new thread for it. – tversteeg Aug 28 '14 at 13:22
  • It may help to set LD_LIBRARY_PATH so it points to the directory that contains libpng16.so.16, before running your program. – Glenn Randers-Pehrson Sep 23 '14 at 22:23
  • -L/usr/local/lib -lpng on Ubuntu – Adam Jun 05 '16 at 15:16
0

I don't know english very well, but I will try to help you.
I was getting this issues about 2 days, the solution is so simple, let's see.

try to do this: gcc my_code.c -lpng
afterwards you will get some file like a.out, then you need to have some picture png in same directory.

after, type this: ./a.out png_file.png res.png

The output file will be called res.png.

toydarian
  • 4,246
  • 5
  • 23
  • 35
-1

I think you installed only the PNG processing library. You will have to install the header files that can reference the library installed. So Install 'dev' package also like this

 sudo apt-get install libpng12-dev 
Mukund K Roy
  • 175
  • 1
  • 8