Can't access cairo.h

apt-file search gives the information

$ apt-file search --regex  /cairo.h$
libcairo2-dev: /usr/include/cairo/cairo.h

Because of that execute

sudo apt install libcairo2-dev

and compile with

g++ screenshot.cpp $(pkg-config --libs --cflags cairo)

Unless you have a need for a Cairo version different from what Ubuntu supplies, please follow A.B.'s answer.

If you want to use the Cairo you installed manually, do as follows.

The problem is that libcairo installs its cairo.h to /usr/local/include/cairo/ and not /usr/local/include/ (i.e. one directory deeper)

You must pass this directory to the compiler with the -I switch.

g++ -I/usr/local/include/cairo/ -o screenshot screenshot.cpp

You will probably run into a linker error then -- the linker doesn't know to search for libcairo and errors on unresolved symbols. So let's give g++ a couple of more parameters.

g++ -I/usr/local/include/cairo/ -L/usr/local/lib -o screenshot screenshot.cpp -lcairo

-lcairo tells the linker to search for a library called cairo and -L/usr/local/lib gives the linker an extra directory to search from.

Note that the parameter order matters with -l -- it should be placed after the source or object files.[1] (In this case, after screenshot.cpp)

This should be enough for compiling your binary.


pkg-config is a tool for automating these things. It gives you the command-line parameters necessary to compile a program using a specific library. I think it often overshoots and ends up linking against multiple libraries that aren't actually needed. The manual way is better in that matter.


[1] Or so I think. I honestly can't grasp what that manual page of GCC is trying to say.