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圖片:

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章