简体   繁体   English

如何在C中包含和使用cairo图形库?

[英]How to include and use cairo graphics library in C?

I have recently downloaded and installed Cairo graphics library for C from the project website . 我最近从项目网站下载并安装了C的Cairo图形库。

I tried to run the hello world program of Cairo by using the given code from the site FAQ . 我尝试使用网站常见问题解答中的给定代码来运行开罗的hello world程序。 In Terminal, I applied the same command as given by the same page to compile it. 在Terminal中,我应用了同一页面给出的相同命令来编译它。 But when I tried to compile it, errors of undefined references appeared. 但是当我尝试编译它时,出现了未定义引用的错误。

在此输入图像描述

In the Terminal, the output is: 在终端中,输出是:

 cc -o hello $(pkg-config --cflags --libs cairo) hello.c
 /tmp/cco08jEN.o: In function `main':
 hello.c:(.text+0x1f): undefined reference to `cairo_image_surface_create'
 hello.c:(.text+0x2f): undefined reference to `cairo_create'
 hello.c:(.text+0x4e): undefined reference to `cairo_select_font_face'
 hello.c:(.text+0x6d): undefined reference to `cairo_set_font_size'
 hello.c:(.text+0x89): undefined reference to `cairo_set_source_rgb'
 hello.c:(.text+0xbb): undefined reference to `cairo_move_to'
 hello.c:(.text+0xcc): undefined reference to `cairo_show_text'
 hello.c:(.text+0xd8): undefined reference to `cairo_destroy'
 hello.c:(.text+0xe9): undefined reference to `cairo_surface_write_to_png'
 hello.c:(.text+0xf5): undefined reference to `cairo_surface_destroy'
 collect2: error: ld returned 1 exit status

And my source code is: 我的源代码是:

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

as described from the site FAQ. 如网站常见问题所述。

I am a beginner in using Terminal commands, and Cairo is the first third party library I used for graphics. 我是使用终端命令的初学者,而Cairo是我用于图形的第一个第三方库。 I tried to find any fix from the Internet, but I didn't get any clue nor fix. 我试图从互联网上找到任何修复,但我没有得到任何线索也没有修复。

Please tell me my error, and explain to me how to use the libraries. 请告诉我我的错误,并向我解释如何使用这些库。

Do this instead: 改为:

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

Let's take a quote from the book, An Introduction to GCC - for the GNU compilers gcc and g++ . 让我们从“ GCC简介 ”一书中引用GNU编译器gcc和g ++

The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. 链接器的传统行为是在命令行中指定的库中从左到右搜索外部函数。 This means that a library containing the definition of a function should appear after any source files or object files which use it. 这意味着包含函数定义的库应该出现在使用它的任何源文件或目标文件之后。 This includes libraries specified with the shortcut -l option. 这包括使用快捷方式-l选项指定的库。

Given that information, doing: 鉴于这些信息,做:

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

would mean that hello.c would not be able to get the function definitions of the Cairo graphics library. 意味着hello.c将无法获得Cairo图形库的函数定义。

On the other hand, if you do this: 另一方面,如果你这样做:

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

would mean that hello.c would be able to get the functions definitions of the Cairo graphics library. 意味着hello.c将能够获得Cairo图形库的函数定义。 Take note that the command above is equivalent to cc -o hello hello.c $(pkg-config --cflags --libs cairo) . 请注意,上面的命令相当于cc -o hello hello.c $(pkg-config --cflags --libs cairo)

More information here , and here . 更多的信息在这里 ,并在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM