6

I've installed cairo-1.4.14 using make install, but after trying to compile my code:

fatal error: cairo.h: No such file or directory
 #include <cairo.h>
                   ^

I compile using this:

g++ screenshot.cpp

I installed 3 packages from this output, but still the same problem:

apt-file search --regex /cairo.h$
libcairo2-dev: /usr/include/cairo/cairo.h
r-cran-rgtk2: /usr/lib/R/site-library/RGtk2/include/RGtk2/cairo.h
thunderbird-dev: /usr/include/thunderbird/cairo/cairo.h

Info about system:

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:    14.04
Codename:   trusty

Output of pkg-config --libs --cflags cairo :

-I/usr/local/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12  -L/usr/local/lib -lcairo
  • You need the .h file in your directory. – Star OS Dec 6 '15 at 10:33
  • 2
    Please, edit the question and include essential parts, example how do you compile ? see askubuntu.com/help/how-to-ask – user.dz Dec 6 '15 at 10:33
  • 1
    @Star OS, if I'd written "cairo.h" then it should be in my directory. – R S Dec 6 '15 at 10:43
  • 1
    @Star OS for what purpose then I installed library in my system, I don't want it to be so. – R S Dec 6 '15 at 10:46
  • 2
    The output of pkg-config --libs --cflags cairo – A.B. Dec 6 '15 at 11:14
4

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.

  • g++ -I/usr/local/include/cairo/ -L/usr/local/lib -lcairo -lX11 screenshot.cpp /tmp/ccvzQc8U.o: In function `main': screenshot.cpp:(.text+0x15): undefined reference to `XOpenDisplay' screenshot.cpp:(.text+0x83): undefined reference to `cairo_xlib_surface_create' screenshot.cpp:(.text+0x98): undefined reference to `cairo_surface_write_to_png' screenshot.cpp:(.text+0xa4): undefined reference to `cairo_surface_destroy' – R S Dec 6 '15 at 19:40
  • and why "against multiple libraries" - if I specify one library ? – R S Dec 6 '15 at 19:54
  • @RS pkg-config may return multiple libraries on a single invocation. pkg-config --libs gtk+-2.0 returns 14 libraries for me, for example. – oals Dec 7 '15 at 12:54
  • @oals cpp you'd better place file name before options, I was unable to execute your command because of this. – R S Dec 8 '15 at 14:09
  • @RS Okay, fixed. – oals Dec 8 '15 at 14:28
7

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)
  • question is updated – R S Dec 6 '15 at 10:53
  • I think you should mention in your answer that it will pull some unnecessary libraries. – R S Dec 8 '15 at 14:11
  • @RS I think you're still a bit confused even after the lengthy discussion on the other question; the -I flags from pkg-config only add to the include file search path. These are harmless, no matter how many there are. Only the -l flag links libraries. And pkg-config only returns a single -l invocation for cairo. Which is just the right amount. – oals Dec 8 '15 at 17:44
  • @oals Then what are you trying to say in the last paragraph of your answer ? – R S Dec 8 '15 at 19:58
  • "If this sounds too complex, you can use pkg-config instead. I do not think it is always optimal though." [it happens to be optimal in the case of cairo. it may be different for others. gtk2 is one example where it is different.] – oals Dec 9 '15 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.