【发布时间】:2026-02-07 20:35:01
【问题描述】:
我最近从project website 下载并安装了 C 的 Cairo 图形库。
我尝试使用来自站点FAQ 的给定代码来运行开罗的hello world 程序。在终端中,我应用了与同一页面给出的相同命令来编译它。但是当我尝试编译它时,出现了未定义引用的错误。
在终端中,输出为:
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
而我的源代码是:
#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;
}
如网站常见问题解答所述。
我是使用终端命令的初学者,Cairo 是我用于图形的第一个第三方库。我试图从 Internet 上找到任何解决方法,但我没有得到任何线索或解决方法。
请告诉我我的错误,并向我解释如何使用这些库。
【问题讨论】:
-
请将这些屏幕截图作为格式化文本发布在问题中。
-
您遇到了链接问题。正如@WeatherVane 所说,发布您如何构建和尝试运行它的详细信息。
-
屏幕截图掉下来了,因为在
cairo_create之前肯定有一个错误,来自cairo_image_surface_create。它没有达到链接:编译器找不到头文件<cairo.h>。您必须在某处设置标题和库的路径。