How to enable and use cairo graphics library in C?

I recently downloaded and installed the Cairo Graphics Library for C on a website.

I tried to launch a peace welcome program in Cairo using this code from the FAQ site . In the terminal, I applied the same command as on the same page to compile it. But when I tried to compile it, undefined link errors appeared.

enter image description here

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:

#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 on the FAQ website.

I'm starting to use terminal commands, and Cairo is the first third library that I used for graphics. I tried to find any fix from the Internet, but I did not understand and did not understand.

Please tell me my mistake and explain to me how to use the libraries.

+4
1

:

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

" GCC" GNU gcc g++.

, . , , , , . , -l.

, :

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

, hello.c .

, :

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

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

.

+12

Source: https://habr.com/ru/post/1621806/


All Articles