Header files in subdirectories (e.g. gtk/gtk.h vs

2019-06-28 00:45发布

I'm trying to build a hello world using GTK, which includes the line:

#include <gtk/gtk.h>

as you would expect.

The Makefile supplied has the line:

GTK_INCLUDE = -I/usr/local/include

so it would expect to find gtk.h in /usr/local/include/gtk/gtk.h. However on my system, it is located in /usr/local/include/gtk-2.0/gtk/gtk.h, ie within a version'ed subdirectory.

Obviously in this case I can add -I/usr/local/include/gtk-2.0 to the Makefile, but the same problem crops up with gtk.h's dependencies and so on.

Is there a good way of dealing with this? Could configure be used to locate where the header files are and add the appropriate include directories? I know next to nothing about configure, but it seems to find out things about the system at build time, which is what I am after.

Is this a common occurence or do I have some freak directory structure which is the real problem?

Thanks for any pointers!

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-28 01:05

You need to use pkg-config to get the include paths:

$ pkg-config --cflags gtk+-2.0
-I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12

You must also use it to get the libraries:

$ pkg-config --libs gtk+-2.0
-lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lgio-2.0 -lcairo -lpango-1.0 -lfreetype -lz -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lglib-2.0

(The output of these commands will vary depending on your distribution, and will always be the correct ones for your distribution.)

查看更多
成全新的幸福
3楼-- · 2019-06-28 01:14

Probably, you must create a symbolic link like:

ln -s /usr/local/include/gtk /usr/local/include/gtk-2.0

but you can first try to reinstall the GTK package.

查看更多
一纸荒年 Trace。
4楼-- · 2019-06-28 01:17

I haven't used gtk in a long while, but the way this is normally handled in Linux is that there is a script called packagename-config (in this case, probably gtk-config) that comes with the development headers, which your makefile is supposed to call in order to get the proper include paths and linker flags for the package, using --cflags and --libs respectively.

So try something like

GTK_INCLUDE=`gtk-config --cflags`

(note the use of backticks, not apostrophes)

And you probably also want to add the output of gtk-config --libs to your LDFLAGS in order to make sure you are linking against all the right stuff.

查看更多
登录 后发表回答