2017-08-10 109 views
1

我安裝了png ++,libpng的包裝器,它恰好適用於C程序(我猜)在那裏我需要使用libpng來完成用C++編寫的項目,所以我安裝了png ++ 2.7使用png ++編寫png圖像

documentation page上,我試着編譯這個例子來檢查包裝器是否安裝正確。這裏是一段代碼:

#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; 
} 

我使用下面的命令編譯上述代碼

pngtest.cpp:

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

+1

這是什麼所指出的@Galik'的libpng-配置--cflags'回報?也許有另一個鏈接器標誌選項。 – tadman

+2

你需要從你鏈接的說明頁面添加鏈接標誌:http://www.nongnu.org/pngpp/doc/0.2.7/'''libpng-config --ldflags \'' – Galik

+0

嗨Galik,謝謝爲更正。我的代碼現在可用。我是C++的初學者,沒有注意到錯誤的鏈接器標誌 – gajendra

回答

1

這是使用鏈接器錯誤標誌的愚蠢錯誤。

正確編譯命令應該是:

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

在評論鏈接是-cflags而不是-ldflags