如何正确安装libpng?

时间:2014-08-28 09:06:52

标签: c ubuntu linker libpng

我正在尝试访问我的C代码中的png像素数据。我找到了这个库libpng。我从这个网站下载了最新版本,我使用的是Ubuntu 14.04。我按照INSTALL文件中的说明操作。一切顺利。然后我尝试使用gcc编译这段code。但我收到了这个:

/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

我不理解它,因为我希望如果安装有问题,我会因为包含png.h而出错。

3 个答案:

答案 0 :(得分:15)

您在评论中说您使用gcc my_code.c,请尝试

gcc my_code.c -lpng

-l标记链接库,在本例中为libpng12-dev

链接意味着您的编译器会添加所有目标文件中的代码以创建单个可执行文件。目标文件是单独编译的源代码文件(.o文件)。

答案 1 :(得分:0)

我不太会英语,但我会尽力帮助您。
我大约两天就收到了这个问题,解决方法非常简单,让我们看看。

尝试执行以下操作:gcc my_code.c -lpng
之后,您将获得诸如a.out之类的文件,那么您需要在同一目录中拥有一些图片png

之后,键入以下内容:./a.out png_file.png res.png

输出文件将称为res.png

答案 2 :(得分:-1)

我认为您只安装了PNG处理库。您必须安装可以引用已安装库的头文件。所以安装' dev'包也喜欢这个

 sudo apt-get install libpng12-dev