Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include and use cairo graphics library in C?

Tags:

c

cairo

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.

like image 795
Nimit Bhardwaj Avatar asked Dec 26 '15 19:12

Nimit Bhardwaj


People also ask

What is C Cairo?

Cairo is a library for creating 2D vector graphics. It is written in the C programming language. There are bindings for other computer languages. Python, Perl, C++, C# or Java.

Does Cairo use GPU?

Cairo is a high-level canvas drawing model for laying out pages and other views. The base elements are resolution independent paths and patterns. Cairo can translate that canvas model into GPU primitives using OpenGL (among others), but the canvas model does not often translate efficiently to the GPU.

What is Cairo package?

Cairo.capabilities() Details. The Cairo package provides multiple back-ends, such as images (PNG, JPEG, TIFF), vector graph- ics (PDF, PostScript, SVG) or displays (X11, Windows).

What is Cairo file?

Cairo (stylized as cairo) is an open-source graphics library that provides a vector graphics-based, device-independent API for software developers. It provides primitives for two-dimensional drawing across a number of different back ends. Cairo uses hardware acceleration when available.


1 Answers

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.

like image 147
Sean Francis N. Ballais Avatar answered Sep 28 '22 07:09

Sean Francis N. Ballais