【问题标题】:How do I install and use PNGwriter to write PNG image files?如何安装和使用 PNGwriter 编写 PNG 图像文件?
【发布时间】:2021-04-15 00:45:38
【问题描述】:

我想学习如何使用 C++ 的 RGB 和 HSV 颜色模型逐个像素地编写 PNG 图像。我读到使用 PNGwriter (https://github.com/pngwriter/pngwriter) 应该相当容易,但我花了很多时间努力安装它(在 Ubuntu 上)并用它编译我的代码。任何帮助将不胜感激。

免责声明:我有一个奇怪的背景,因为我在使用类 Unix 操作系统、在终端中做事和编写代码方面有多年的经验,但我知道的很少/从源代码安装软件或手动编译程序或使用来自多个源代码文件的 makefile 没有任何意义。

GitHub 上的安装说明建议执行以下操作之一:

打包:

   spack install pngwriter
   spack load pngwriter

来源:

首先安装依赖项 zlib、libpng 和(文本支持可选)freetype。 PNGwriter >然后可以使用 CMake 安装:

   git clone https://github.com/pngwriter/pngwriter.git
   
   mkdir -p pngwriter-build
   cd pngwriter-build
   
   # for own install prefix append: -DCMAKE_INSTALL_PREFIX=$HOME/somepath
   cmake ../pngwriter
   
   make -j
   
   # optional
   make test
   
   # sudo is only required for system paths
   sudo make install

我设法安装了 Spack,然后安装了 PNGwriter,但无法用它编译最简单的程序,也无法弄清楚原因。然后我手动安装了 PNGwriter,但仍然无法用它编译任何东西。这是很多小时前的挣扎,所以很遗憾,我不记得此时我遇到了什么样的错误或问题。

GitHub 上的说明关于链接如下:

如果系统路径中没有安装PNGwriter,首先设置如下环境提示:

   # optional: only needed if installed outside of system paths
   export CMAKE_PREFIX_PATH=$HOME/somepath:$CMAKE_PREFIX_PATH

在您的项目 CMakeLists.txt 中使用以下行:

   find_package(PNGwriter 0.7.0)
   
   if(PNGwriter_FOUND)
     target_link_libraries(YourTarget PRIVATE PNGwriter::PNGwriter)
   endif(PNGwriter_FOUND)

问题:我如何知道系统路径中是否安装了 PNGwriter?我在/usr/local/lib 中有libPNGwriter.a,在/usr/local/include 中有pngwriter.h ---这是否意味着它安装在系统路径中?安装时,我只是尝试按照上面的说明进行操作。我只是在终端中输入环境提示还是将其添加到某个文件中?如果是前者,那么每次我打开一个新的终端会话时都需要给出它吗?是“somepath”/usr/local/lib/usr/local/include 还是别的什么?

问题:关于“CMakeLists.txt”的第二部分是否取决于PNGwriter 是否安装在系统路径中?什么是“CMakeLists.txt”?我认为这是 IDE 创建的某个文件,但我的 NetBeans 项目似乎不包含此类文件。如果我有一个源文件并在终端中手动编译它会怎样?

现在,假设我要编译 PNGwriter 快速入门示例:

   #include <pngwriter.h>
   
   int main()
   {
     int i;
     int y;
     pngwriter png(300, 300, 0, "test.png");
     for(i = 1; i ≤ 300; i++)
     {
       y = 150 + 100*sin((double)i*9/300.0);
       png.plot(i, y, 0.0, 0.0, 1.0);
     }
     png.close();
     return 0;
   }

PNGwriter 手册指示编译为

    g++ myprogram.cc -o my_program `freetype-config --cflags` -I/usr/local/include -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype

但我得到错误

    $ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include  -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype
    Package freetype2 was not found in the pkg-config search path.
    Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
    Package 'freetype2', required by 'virtual:world', not found
    Package freetype2 was not found in the pkg-config search path.
    Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
    Package 'freetype2', required by 'virtual:world', not found
    Package freetype2 was not found in the pkg-config search path.
    Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
    Package 'freetype2', required by 'virtual:world', not found
    Package freetype2 was not found in the pkg-config search path.
    Perhaps you should add the directory containing `freetype2.pc'
to the PKG_CONFIG_PATH environment variable
    Package 'freetype2', required by 'virtual:world', not found
    sed: -e expression #1, char 0: no previous regular expression
    sed: -e expression #1, char 0: no previous regular expression
    In file included from example.cpp:1:0:
    /usr/local/include/pngwriter.h:66:22: fatal error: ft2build.h: No such file or directory
    compilation terminated.

此 Stack Overflow 问题 (Trying to install pygame on ubuntu which gives error) 的答案建议安装 libfreetype6-dev,但我显然已经拥有最新版本,因此错误保持不变。如果我实际上将包含freetype2.pc 的目录(我通过转到/ 并使用find -name "freetype2.pc" 找到它)添加到环境变量(将export PKG_CONFIG_PATH="/usr/lib/x86_64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH 添加到~/.bashrc 并做了source ~/.bashrc)然后我得到了新的错误

    $ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include  -L/usr/local/lib -lpng -lpngwriter -lz -lfreetype
    /usr/bin/ld: cannot find -lpngwriter
    collect2: error: ld returned 1 exit status

在这里我发现我需要用-lPNGwriter 替换-lpngwriter(即,手册有误)。然后我得到:

    $ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include  -L/usr/local/lib -lpng -lPNGwriter -lz -lfreetype
    /usr/bin/ld: /usr/local/lib/libPNGwriter.a(pngwriter.cc.o): undefined reference to symbol 'png_set_sig_bytes@@PNG12_0'
    /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libpng.so: error adding     symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status

这是我目前卡住的地方。我似乎无法通过谷歌搜索找到解决方案(至少我能理解)。

问题:我如何让它发挥作用?如何让它在 NetBeans 中工作?我是否因为完成了上述链接步骤而遇到这些问题?

Edit1:根据 john 的评论,我尝试交换 -lpng-lPNGwriter 并再次遇到新错误:

    $ g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include  -L/usr/local/lib -lPNGwriter -lpng -lz -lfreetype
    /usr/local/lib/libPNGwriter.a(pngwriter.cc.o): In function `pngwriter::close()':
    pngwriter.cc:(.text+0x41c2): undefined reference to `png_convert_to_rfc1123_buffer'
    /usr/local/lib/libPNGwriter.a(pngwriter.cc.o): In function `pngwriter::read_png_info(_IO_FILE*, png_struct_def**, png_info_def**)':
    pngwriter.cc:(.text+0x4fdf): undefined reference to `png_set_longjmp_fn'
    collect2: error: ld returned 1 exit status

我还是一头雾水。

【问题讨论】:

  • cmake 使用 CMakeLists.txt 文件,这仅在您从源代码安装时才相关,您似乎没有这样做。所以我会忽略那部分。
  • 对于您的最后一个错误,我会尝试颠倒库的顺序 -lPNGwriter -lpng 而不是相反。
  • 我想我设法使用 Spack 和从源代码进行了安装,但后者更有可能以某种方式出错。
  • @john,感谢您的建议,我再次收到新的错误,这些错误已编辑到我的问题中。
  • 最新问题似乎是版本冲突stackoverflow.com/questions/21545076/… 但我不是专家。我会重新开始,我会避免尝试从源代码安装。第一个选择,你的普通 Ubuntu 包管理器(例如 apt),第二个选择 spack,第三个来自源代码的选择。

标签: c++ ubuntu


【解决方案1】:

感谢 john,我想我明白了。我的猜测是从源代码安装(在安装 Spack 之后)以某种方式搞砸了。我使用 Spack 重新安装了 PNGwriter,现在显然已经有了编译命令的所有部分,终于能够编译示例代码了。

总结

源码包

    # For bash/zsh/sh
    $ . spack/share/spack/setup-env.sh
    
    # For tcsh/csh
    $ source spack/share/spack/setup-env.csh
    
    # For fish
    $ . spack/share/spack/setup-env.fish

使用 Spack 安装(如果已安装则跳过)

    spack install pngwriter

加载 PNGwriter(我想在每个新的终端会话中都需要这样做)

    spack load pngwriter

编译(注意这不是 PNGwriter 手册所建议的)

    g++ example.cpp -o example `freetype-config --cflags` -I/usr/local/include  -L/usr/local/lib -lPNGwriter -lpng -lz -lfreetype

【讨论】:

    猜你喜欢
    • 2018-01-19
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多