How to include and use cairo graphics library in C?
Asked Answered
C

1

8

I have recently downloaded and installed Cairo graphics library for C from the project website.

I tried to run the hello world program of Cairo by using the given code from the site FAQ. In Terminal, I applied the same command as given by the same page to compile it. But when I tried to compile it, errors of undefined references 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 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. 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.

Calista answered 26/12, 2015 at 19:10 Comment(3)
Please post those screen shots as formatted text in the question.Isreal
You have a linking problem. As @WeatherVane says, post the details of how you built and tried to run it.Donata
The screen shot falls down because there must have been a previous error before cairo_create, from cairo_image_surface_create. It does not get as far as linking: the compiler cannot find the header file <cairo.h>. You have to set up the paths to headers and library somewhere.Isreal
B
21

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++.

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.

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.

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. Take note that the command above is equivalent to cc -o hello hello.c $(pkg-config --cflags --libs cairo).

More information here, and here.

Bohon answered 27/12, 2015 at 9:10 Comment(1)
Very Very thank you sir this worked, again thanks for your help.Calista

© 2022 - 2024 — McMap. All rights reserved.