gpt4 book ai didi

c++ - 使用 png++ 编写 png 图像

转载 作者:行者123 更新时间:2023-11-30 05:10:33 25 4
gpt4 key购买 nike

我安装了 png++,它是 libpng 的包装器,恰好用于 C 程序(我猜),至于光线追踪图像生成,我需要将 libpng 用于完全用 C++ 编写的项目,所以我安装了 png++ 2.7

关于 documentation page ,我尝试编译示例以检查包装器是否安装正确。这是一段代码:

pngtest.cpp

#include <png++/png.hpp>
#include <iostream>

int main()
{

std::cout << "\n***********************************\n";
std::cout << "Testing libpng";

png::image< png::rgb_pixel > image(640, 480);
std::cout <<"Image width: " << image.get_width() <<"\n";
std::cout << "Image height: " << image.get_height() <<"\n";

for (size_t y = 0; y < image.get_height(); ++y)
{
for (size_t x = 0; x < image.get_width(); ++x)
{
//image[y][x] = png::rgb_pixel(x, y, x + y);
// non-checking equivalent of image.set_pixel(x, y, ...);
}
}

image.write("rgb.png");
std::cout << "\n***********************************\n";

return 0;
}

我使用以下命令编译了上面的代码:

g++  pngTest.cpp `libpng-config --cflags` -o pngTest.out

这是我得到的错误:

pngTest.cpp:(.text._ZN3png9info_baseC2ERNS_7io_baseEP14png_struct_def[_ZN3png9info_baseC5ERNS_7io_baseEP14png_struct_def]+0x37): undefined reference to `png_create_info_struct'
/tmp/ccgfdQ53.o: In function `png::info::write() const':
pngTest.cpp:(.text._ZNK3png4info5writeEv[_ZNK3png4info5writeEv]+0xcc): undefined reference to `png_set_PLTE'
pngTest.cpp:(.text._ZNK3png4info5writeEv[_ZNK3png4info5writeEv]+0x12d): undefined reference to `png_set_tRNS'
pngTest.cpp:(.text._ZNK3png4info5writeEv[_ZNK3png4info5writeEv]+0x148): undefined reference to `png_write_info'
/tmp/ccgfdQ53.o: In function `png::info::sync_ihdr() const':
pngTest.cpp:(.text._ZNK3png4info9sync_ihdrEv[_ZNK3png4info9sync_ihdrEv]+0x6e): undefined reference to `png_set_IHDR'
/tmp/ccgfdQ53.o: In function `png::end_info::destroy()':
pngTest.cpp:(.text._ZN3png8end_info7destroyEv[_ZN3png8end_info7destroyEv]+0x49): undefined reference to `png_destroy_info_struct'
/tmp/ccgfdQ53.o: In function `png::end_info::write() const':
pngTest.cpp:(.text._ZNK3png8end_info5writeEv[_ZNK3png8end_info5writeEv]+0x23): undefined reference to `png_write_end'
/tmp/ccgfdQ53.o: In function `png::io_base::set_swap() const':
pngTest.cpp:(.text._ZNK3png7io_base8set_swapEv[_ZNK3png7io_base8set_swapEv]+0x26): undefined reference to `png_set_swap'
/tmp/ccgfdQ53.o: In function `png::io_base::set_interlace_handling() const':
pngTest.cpp:(.text._ZNK3png7io_base22set_interlace_handlingEv[_ZNK3png7io_base22set_interlace_handlingEv]+0x26): undefined reference to `png_set_interlace_handling'
/tmp/ccgfdQ53.o: In function `png::io_base::raise_error()':
pngTest.cpp:(.text._ZN3png7io_base11raise_errorEv[_ZN3png7io_base11raise_errorEv]+0x21): undefined reference to `png_set_longjmp_fn'
/tmp/ccgfdQ53.o: In function `png::io_base::raise_error(png_struct_def*, char const*)':
pngTest.cpp:(.text._ZN3png7io_base11raise_errorEP14png_struct_defPKc[_ZN3png7io_base11raise_errorEP14png_struct_defPKc]+0x18): undefined reference to `png_get_error_ptr'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::writer(std::basic_ofstream<char, std::char_traits<char> >&)':
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEEC2ERS4_[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEEC5ERS4_]+0x28): undefined reference to `png_create_write_struct'
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEEC2ERS4_[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEEC5ERS4_]+0x57): undefined reference to `png_set_write_fn'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::~writer()':
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEED2Ev[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEED5Ev]+0x3b): undefined reference to `png_destroy_write_struct'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::write_info() const':
pngTest.cpp:(.text._ZNK3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_infoEv[_ZNK3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_infoEv]+0x24): undefined reference to `png_set_longjmp_fn'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::write_row(unsigned char*)':
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE9write_rowEPh[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE9write_rowEPh]+0x28): undefined reference to `png_set_longjmp_fn'
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE9write_rowEPh[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE9write_rowEPh]+0x83): undefined reference to `png_write_row'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::write_end_info() const':
pngTest.cpp:(.text._ZNK3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE14write_end_infoEv[_ZNK3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE14write_end_infoEv]+0x24): undefined reference to `png_set_longjmp_fn'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::write_data(png_struct_def*, unsigned char*, unsigned long)':
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_dataEP14png_struct_defPhm[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_dataEP14png_struct_defPhm]+0x1d): undefined reference to `png_get_error_ptr'
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_dataEP14png_struct_defPhm[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10write_dataEP14png_struct_defPhm]+0x41): undefined reference to `png_get_io_ptr'
/tmp/ccgfdQ53.o: In function `png::writer<std::basic_ofstream<char, std::char_traits<char> > >::flush_data(png_struct_def*)':
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10flush_dataEP14png_struct_def[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10flush_dataEP14png_struct_def]+0x15): undefined reference to `png_get_error_ptr'
pngTest.cpp:(.text._ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10flush_dataEP14png_struct_def[_ZN3png6writerISt14basic_ofstreamIcSt11char_traitsIcEEE10flush_dataEP14png_struct_def]+0x39): undefined reference to `png_get_io_ptr'
collect2: error: ld returned 1 exit status

谁能说出哪里出了问题。我使用的是 Ubuntu 16.04 LTS,我系统上安装的 libpng 版本是 1.6

最佳答案

这是为链接器使用错误标志的愚蠢错误。

正确的编译命令应该是:

g++  pngTest.cpp `libpng-config --ldflags` -o pngTest.out

正如@Galik 在评论中指出的那样,链接器是 -cflags 而不是 -ldflags

关于c++ - 使用 png++ 编写 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com