Finding include directories and link libraries

D

Dave Farrance

What's the correct way to find the include directories and link
libraries when compiling with gnu c++ ?

I decided that I'd like to try out line-drawing using the GTK toolkit,
so I copied the line drawing examples from this tutorial page:
http://www.gtkmm.org/tutorial/sec-drawingarea.html
I then tried to compile it with c++ on Mandrake Linux, and spent ages
resolving the dependencies.

I ran the compiler, looked at the first complaint about a header file
that it couldn't include, used the "locate" command to find it on my
machine, and if it wasn't found, search for the package containing that
file to be downloaded, and then add that directory as a compiler include
directive. This had to be repeated several times.

Then the linking. At each complaint about an unresolved function, I
tried to guess the library that was required from the name of the
function, or from the names of the previously worked out include files,
and from the names of libraries in the /usr/lib directory. I usually
stumbled onto the correct library after a few attempts in each case.

After all that work, my makefile ended up looking like this:

gtktest : gtktest.cc
c++ gtktest.cc -o gtktest \
-I/usr/include/gtk-1.2 -I/usr/lib/gtkmm/include \
-I/usr/include/glib-1.2 -I/usr/lib/glib/include \
-I/usr/include/sigc++-1.0 \
-lgtkmm -lsigc -lgdkmm -lpthread

That was just to try out a tutorial. Surely it shouldn't be that hard?
Am I going about this the wrong way?
 
T

Thomas Maier-Komor

Dave said:
What's the correct way to find the include directories and link
libraries when compiling with gnu c++ ?

I decided that I'd like to try out line-drawing using the GTK toolkit,
so I copied the line drawing examples from this tutorial page:
http://www.gtkmm.org/tutorial/sec-drawingarea.html
I then tried to compile it with c++ on Mandrake Linux, and spent ages
resolving the dependencies.

I ran the compiler, looked at the first complaint about a header file
that it couldn't include, used the "locate" command to find it on my
machine, and if it wasn't found, search for the package containing that
file to be downloaded, and then add that directory as a compiler include
directive. This had to be repeated several times.

Then the linking. At each complaint about an unresolved function, I
tried to guess the library that was required from the name of the
function, or from the names of the previously worked out include files,
and from the names of libraries in the /usr/lib directory. I usually
stumbled onto the correct library after a few attempts in each case.

After all that work, my makefile ended up looking like this:

gtktest : gtktest.cc
c++ gtktest.cc -o gtktest \
-I/usr/include/gtk-1.2 -I/usr/lib/gtkmm/include \
-I/usr/include/glib-1.2 -I/usr/lib/glib/include \
-I/usr/include/sigc++-1.0 \
-lgtkmm -lsigc -lgdkmm -lpthread

That was just to try out a tutorial. Surely it shouldn't be that hard?
Am I going about this the wrong way?

for gtk, glib, and friends there exist config executables that
can be used to get the flags for compilation and linking.

e.g.:
c++ `gtk-config --cflags --libs` -o gtktest gtktest.cc
 
D

Dave Farrance

for gtk, glib, and friends there exist config executables that
can be used to get the flags for compilation and linking.

e.g.:
c++ `gtk-config --cflags --libs` -o gtktest gtktest.cc

Thanks. That gtk-config generates:
-I/usr/include/gtk-1.2 -I/usr/include/glib-1.2 -I/usr/lib/glib/include
-I/usr/X11R6/include -L/usr/X11R6/lib -lgtk -lgdk -rdynamic -lgmodule
-lglib -ldl -lXi -lXext -lX11 -lm

.... which includes three of the five include directories that I needed,
but none of the libraries, unfortunately. It would have saved me some
work, though. I must watch out for these config files.
 
M

Murray Cumming

Dave Farrance said:
What's the correct way to find the include directories and link
libraries when compiling with gnu c++ ?

I decided that I'd like to try out line-drawing using the GTK toolkit,
so I copied the line drawing examples from this tutorial page:
http://www.gtkmm.org/tutorial/sec-drawingarea.html [snip]
-I/usr/include/gtk-1.2 -I/usr/lib/gtkmm/include \
-I/usr/include/glib-1.2 -I/usr/lib/glib/include \
-I/usr/include/sigc++-1.0 \
-lgtkmm -lsigc -lgdkmm -lpthread
[snip]

You are making your life difficult by using gtkmm 1.2 (and GTK+ 1.2).
It's ancient. The newer versions (e.g. 2.6) have far easier API. And
they use pkg-config to make building much easier - see the gtkmm FAQ.
 
D

Dave Farrance

You are making your life difficult by using gtkmm 1.2 (and GTK+ 1.2).
It's ancient. The newer versions (e.g. 2.6) have far easier API. And
they use pkg-config to make building much easier - see the gtkmm FAQ.

Thanks. I'd used gtkmm 1.2 because that tutorial was the only example
that I found with a websearch on GTK +"line draw".

I searched for that gtkmm FAQ you mentioned, and discovered that
Mandrake Linux had packaged the gtkmm 2.4 documents in a separate RPM.
There's also a substantial tutorial in there as well with a line draw
example that did look cleaner. And pkg-config worked perfectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top