ubuntu Cairo图形库 环境搭建

Cairo官网:http://cairographics.org/

根据官网指示,我使用如下语句安装好cairo库

sudo apt-get install libcairo2-dev

从官网拷贝代码(http://cairographics.org/FAQ/#compilation_flags):hello.c

#include <cairo.h>

int
main (int argc, char *argv[])
{
        cairo_surface_t *surface =
            cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
        cairo_t *cr =
            cairo_create (surface);

        cairo_select_font_face (cr, "serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
        cairo_set_font_size (cr, 32.0);
        cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
        cairo_move_to (cr, 10.0, 50.0);
        cairo_show_text (cr, "Hello, world");

        cairo_destroy (cr);
        cairo_surface_write_to_png (surface, "hello.png");
        cairo_surface_destroy (surface);
        return 0;
}

然后根据官网给出的命令行进行编译

gcc -o hello $(pkg-config --cflags --libs cairo) hello.c

但是结果如下图所示

出现这种“undifined”一般是库文件没有找到,于是执行

pkg-config --cflags --libs cairo


得到的字符串中直接有 -lcairo,而此时查找libcairo.a的路径应该是“/usr/lib”,但是我到该目录下查看,并没有发现libcairo.a库文件。

再仔细查看官方文档(http://cairographics.org/download/

Many distributions including Debian,Fedora, and others regularly include recent versions of cairo. As more and more applications depend on cairo, you might find that the library is already installed. To get the header files installed as well may require asking for a -dev or-devel package as follows:

许多像Debian、Fedora这样的发行版本通常都包含了最新版本的cairo。而且随着越来越多的应用程序依赖于cairo,你会发现cairo的库文件早已安装在你的系统中。如果需要安装头文件,请使用下面语句:

原来,cairo库文件早就在我的系统中了,刚刚安装的只是头文件,通过文件管理器,我找到了libcairo.a:

所以我只需要告知编译器,我的库文件在这就行了,由于我对“pk-config”工具不熟悉,所以使用如下语句进行编译:

gcc -o hello hello.c -L /usr/lib/i386-linux-gnu/ -lcairo -I /usr/include/cairo

然后运行hello得到一张png图片:

hello world:



原文地址:https://www.cnblogs.com/zhanghang-BadCoder/p/6476462.html