Can't access cairo.h
Solution 1
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.
Solution 2
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)
Related videos on Youtube
R S
Updated on September 18, 2022Comments
-
R S over 1 year
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
-
R S over 8 yearsquestion is updated
-
R S over 8 years
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 over 8 yearsand why "against multiple libraries" - if I specify one library ?
-
oals over 8 years@RS
pkg-config
may return multiple libraries on a single invocation.pkg-config --libs gtk+-2.0
returns 14 libraries for me, for example. -
R S over 8 years@oals cpp you'd better place file name before options, I was unable to execute your command because of this.
-
R S over 8 yearsI think you should mention in your answer that it will pull some unnecessary libraries.
-
oals over 8 years@RS Okay, fixed.
-
R S over 8 years@oals confused, I wanna say better right after g++, the main thing that is will be placed not after -l argument.
-
oals over 8 yearsWell, the thing is, it needs to be
screenshot.cpp -lcairo
, not-lcairo screenshot.cpp
due to the way the linker works. All other parameters can be in any random order as far as I can tell. -
oals over 8 years@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. -
R S over 8 years@oals Then what are you trying to say in the last paragraph of your answer ?
-
oals over 8 years"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.]