How to install libpng correctly?
Asked Answered
I

3

11

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.

Irremovable answered 28/8, 2014 at 9:6 Comment(6)
Do you have libpng12-dev installed? sudo apt-get install libpng12-dev the files required for using it in development is generally in a -dev package on Debian and derivates.Girdle
It seems that you are not linking against the libpng library.Henrik
@Girdle those are generally the header files. OP has a linker error, though, and not a compiler error.Henrik
Show exactly your compilation command. Order of arguments to gcc matters a lot!Easing
Sorry I am just starting writing under linux, I just used gcc my_code.c. @TheParamagneticCroissant what do you mean by linking against the libpng library ?Irremovable
@MichalKrakovsky Just google it.Henrik
D
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).

Delldella answered 28/8, 2014 at 10:41 Comment(9)
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 directoryIrremovable
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.Irremovable
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?Delldella
I compiled it from source I guess, I did it trough make install, not apt-get install.Irremovable
You can use find /usr/lib -name *png* to find the location and then use gcc my_code.c -L<location> -lpng16.Delldella
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.Irremovable
I wouldn't know the solution to this problem, I think you should open a new thread for it.Delldella
It may help to set LD_LIBRARY_PATH so it points to the directory that contains libpng16.so.16, before running your program.Barbirolli
-L/usr/local/lib -lpng on UbuntuPiggin
C
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.

Climb answered 30/7, 2020 at 8:21 Comment(0)
K
-2

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 
Kelci answered 28/8, 2014 at 9:24 Comment(1)
He obviously does have the header files, since he got through compilation. It's the linkage that is failing.Henrik

© 2022 - 2024 — McMap. All rights reserved.