我安装了png ++,一个libpng的包装器,恰好用于C程序(我猜),对于光线跟踪图像生成我需要使用libpng完全用C ++编写的项目,所以我安装了png ++ 2.7 < / p>
在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
答案 0 :(得分:1)
使用错误的标志链接器是一个愚蠢的错误。
正确的编译命令应为:
g++ pngTest.cpp `libpng-config --ldflags` -o pngTest.out
正如@Galik在评论中指出的那样,链接器是-cflags
而不是-ldflags