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.
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.
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)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.