Using gcc, I am unable to get 3rd party library, C

2019-01-28 08:10发布

问题:

The errors that show up are for undefined symbols, not the Cairo functions I copied from the example, but what I guess are internal functions. I have looked around and tried using $(pkg-config --cflags --libs cairo), verbatim from a common online example using the terminal.

I tried that after using EXPORT=(my path to cairos .ps file).

I currently have one option in CodeBlocks 'Link Libraries' in the linker settings: /usr/lib/x86-linux-gnu/libcairo.a

Also, I have /usr/include/cairo in my compiler options.

Errors:

/usr/lib/x86_64-linux-gnu/libcairo.a(cairo-image-source.o)||In function _cairo_image_source_finish':| (.text+0x1c)||undefined reference topixman_image_unref'|

Plus 50 more like it.

Which are generated from the following, main.c:

include cairo.h (with # and <>)

int main()
{

    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;
}

Any help fixing and understanding what's going on would be greatly appreciated. Thanks.

回答1:

So the solution for this problem was as follows:

Use pkg-config for all the correct includes. Do so automatically by adding:

pkg-config --cflags --libs cairo (surrounded by " ` ", on the tilde key.)

to my "Other options" tab in the compiler settings.

I did the same for the linker "Other options" tab using:

pkg-config --libs cairo (also surrounded by " ` ", the accent grave)

Now I build and compile with no errors, but when I run it, I don't see anything but an empty console window : /



回答2:

As you are using a png surface you also need to include 'cairo-png' in your pkconfig line. Try

$(pkg-config --cflags --libs cairo cairo-png)

For a full list of cairo related pkg-config options look in /usr/lib/pkgconfig.

Good luck.

Alwin



回答3:

It clearly means the linker is not able to locate all the required references for the linkage. Specifically, your cairo library function has a dependency on pixman_image_unref which is on a different library and the linker is not able to locate this library. You need to include the pixman library or any other library where the pixman_image_unref is available.



回答4:

just use -lpixman-1. When you give the -L/_path_to_lib/ add -lpixman-1 at the end i.e.

-L/_path_to_lib/ -lpixman-1


回答5:

I have had the same issue and I resolved it without pkg-config, just linking to the libcairo.so under /usr/lib.

g++ -L/usr/lib -lcairo hello.o -o hello

I was first trying with eclipse luna but didnt work so I run it from the terminal using a custom makefile:

CC=g++
CFLAGS=-c -Wall -I/usr/include/cairo
LDFLAGS=-L/usr/lib -lcairo
SOURCES=hello.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=hello

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.c.o:
    $(CC) $(CFLAGS) $< -o $@

hope it helps more.

cheers