c++node.jslinkercairopango

Symbol lookup error when calling pango_cairo_create_layout


Noob alert! I'm not exactly competent with C/C++ programming

Hi, I'm working on a C++ NodeJS addon, in which I'd like to use Cairo/Pango, but I'm having a number of linking issues. I can reasonably assume they're linking issues, as I had a similar one, calling a Cairo function. I was able to resolve it by adding -lcairo to the g++ call, in my makefile. Unfortunately, this isn't working for Pango anymore.

Below is my Makefile

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(dir $(mkfile_path))

SYS=-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/
NODE=-I/usr/include/node -I$(mkfile_dir)node_modules/node-addon-api/
INCLUDE=$(SYS) -I/usr/include/cairo -I/usr/include/pango-1.0
LIBS=$(shell pkg-config --cflags --libs cairo pango)

plugin.node: clean
    g++ -shared -fPIC -o ./plugin.node lib.cpp -Wall -Wextra $(INCLUDE) $(LIBS)

clean:
    $(if $(wildcard ./*.node), rm *.node)

The LIBS variable came from a SO post advising against using -l... options, and instead using pkg-config to locate them. The C++ code is nothing extraordinary, but for completeness, it's below.

#include <cairo.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>

struct Canvas {
    uint16_t width, height;

    cairo_surface_t* surface;
    cairo_t* ctx;
};

// ...
    PangoLayout *layout = pango_cairo_create_layout(canvas->ctx);
    PangoFontDescription *desc = pango_font_description_new();

    pango_layout_set_text (layout, "Test String", 11);
    pango_font_description_set_size(desc, 10);
// ...

The compilation succeeds, with only warnings about unused variables, however, when attempting to load the module in NodeJS, the following error appears:

/usr/bin/node: symbol lookup error: /.../plugin.node: undefined symbol: pango_cairo_create_layout

Edit

When running pkg-config --cflags --libs cairo pango, the following output is produced:

-pthread -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng16 -lcairo -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz

And readelf -d plugin.node


Solution

  • Add pangocairo to your pkg-config invocation:

    LIBS=$(shell pkg-config --cflags --libs cairo pango pangocairo)
    

    It's a separate library and you are missing it.