GTK2.0 is installed but unable to locate when compiling - c

I am brand new to GTK and looking to compile my first program with it. Upon compiling I get the following error:
randall#randall-ubuntu:~/c_programs/bettingCalc$ gcc -o bettingCalc main.c
main.c:8:21: fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
^
compilation terminated.
The typical solution seems to be running the command:
sudo apt-get install libgtk2.0-dev
Which I ran, and seemingly successfully installed. What am I missing here?
if it is at all relevant, here is the last 8 lines of the installation process:
Setting up libxcomposite-dev (1:0.4.4-1) ...
Setting up x11proto-damage-dev (1:1.2.1-2) ...
Setting up libxdamage-dev:amd64 (1:1.1.4-1ubuntu1) ...
Setting up libxml2-utils (2.9.1+dfsg1-3ubuntu4.4) ...
Setting up libgtk2.0-dev (2.24.23-0ubuntu1.1) ...
Setting up libsys-hostname-long-perl (1.4-3) ...
Setting up libmail-sendmail-perl (0.79.16-1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.4) ...
randall#randall-ubuntu:~/c_programs/bettingCalc$

You have to use gtk-config in compile/link cycle to get info about installed GTK
http://manpages.ubuntu.com/manpages/intrepid/man1/gtk-config.1.html
UPDATE: gtk-config is depreciated, please use pkg-config to achieve desired result and get right
includes, flags and library references
For compilation
gcc -c main.c `pkg-config --cflags gtk+-2.0`
For linking
gcc -o app main.o `pkg-config --libs gtk+-2.0`

Related

gdk/gdk.h: No such file or directory

I am trying to use gui with gkt-2.0 in Linux mint 32bit. When I try to compile gui.c I encountered following error message:
#include<gtk-2.0/gtk/gtk.h>
void main(){
}
In file included from gui.c:1:0:
/usr/include/gtk-2.0/gtk/gtk.h:32:10: fatal error: gdk/gdk.h: No such file or directory
#include <gdk/gdk.h>
^~~~~~~~~~~
When the appropriate packages are installed, you can add the needed include search directories with option -I, and the libraries of course -l, e.g.
gcc -I/usr/include/gtk-2.0 gui.c -o gui -lgtk-2.0
The source should be changed to
#include <gtk/gtk.h>
To avoid hard-coding any paths and names, you could use pkg-config (Compiling GTK Applications on UNIX)
gcc $(pkg-config --cflags gtk+-3.0) gui.c -o gui $(pkg-config --libs gtk+-3.0)
Better yet, use make or some other build tool.

Compiling GTK 3 apps on Debian

I'm trying to compile a GTK program on my Debian desktop. I installed libgtk-3-dev and all of that, but when I go to compile the program, I get this error:
$ gcc -o client client.c `pkg-config --cflags --libs glib-2.0`
client.c:6:10: fatal error: gtk/gtk.h: No such file or directory
6 | #include <gtk/gtk.h>
| ^~~~~~~~~~~
compilation terminated.
All of the GTK headers actually seem to be in /usr/include/gtk-3.0, but even if I include <gtk-3.0/gtk/gtk.h> I get errors, since the files inside include other GTK headers as if they were in a normal include path. Then if I compile with -I/usr/include/gtk-3.0 I still get errors, this time about glib. Well, glib files are inside /usr/include/glib-2.0 and it's the same problem as before. Finally, if I compile with -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0, I this time get this error:
$ gcc -o client -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 client.c `pkg-config --cflags $ --libs glib-2.0`
In file included from /usr/include/gtk-3.0/gdk/gdkapplaunchcontext.h:30,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:30,
from client.c:6:
/usr/include/gtk-3.0/gdk/gdktypes.h:35:10: fatal error: pango/pango.h: No such file or directory
35 | #include <pango/pango.h>
| ^~~~~~~~~~~~~~~
compilation terminated.
Seems like any combination of compile flags I choose, I get some sort of error, all caused by Debian putting the headers in a non-standard subdirectory. This is not me messing with them, this is what apt installs for me.
You are asking pkg-config for glib, you must ask pkg-config for the GTK+ library:
gcc -o client client.c `pkg-config --cflags --libs gtk+-3.0`
You can keep glib-2.0 in the command line, but since GTK+ depends on glib, it's already included.

Compile hiredis in C on Mac OS X

I'm trying to compile a client using hiredis in C on Mac OS X.
I've installed hiredis with:
brew install hiredis
But still get the error:
fatal error: 'hiredis.h' file not found
My hiredis.h is however in:
/usr/local/include/hiredis/hiredis.c
How do I tell the compiler this?
I'm compiling with:
gcc test.c -o test
In your question you said hiredis.h is in /usr/local/include/hiredis/hiredis.c, which doesn't really make any sense.
Assuming you meant that your hiredis.h is in /usr/local/include/hiredis. You can do like:
gcc test.c -I/usr/local/include/hiredis -o test
Read about -I in this SO post.
UPDATE:
As mentioned by #EricPostpischil in comments, its a better idea to just include like:
#include < hiredis/hiredis.h>
I am still not sure if /usr/local/include is in default include path. If it is, well no need to do anything, just compile like:
gcc test.c -o test
and if it isn't,
gcc test.c -I/usr/local/include -o test
If you have installed hiredis with homebrew, you can see what's in the package like this:
brew ls --verbose hiredis
/usr/local/Cellar/hiredis/0.14.0/INSTALL_RECEIPT.json
/usr/local/Cellar/hiredis/0.14.0/CHANGELOG.md
/usr/local/Cellar/hiredis/0.14.0/.brew/hiredis.rb
...
...
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.dylib
/usr/local/Cellar/hiredis/0.14.0/lib/pkgconfig/hiredis.pc <--- PKG-CONFIG
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.a
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.0.14.dylib
...
...
And, as you can see, it gives you a pkg-config file with all the settings in it that you need. So, you might as well install pkg-config and do it properly!
brew install pkg-config
Now, if you want to know the C compiler flags for hiredis, you do:
pkg-config --cflags hiredis
-D_FILE_OFFSET_BITS=64 -I/usr/local/Cellar/hiredis/0.14.0/include/hiredis
And if you want to know the linker settings, you do:
pkg-config --libs hiredis
-L/usr/local/Cellar/hiredis/0.14.0/lib -lhiredis
And so, your compile-link command becomes very simple and updates itself when you update the packages:
gcc-9 $(pkg-config --cflags --libs hiredis) -o program program.c

Compiling in c: include file not found

I got following error
'libwebsockets.h' file not found
but I have installed libwebsockets with the command
brew install libwebsockets
How can I solve this error?
I want to implement a websocketserver and i need this.
If my code is only has this following line
#include <libwebsockets.h>
it gave me an error.
I tried to compile it with gcc foo.c
You need to pass the appropriate compile and link flags to the compiler for it to find the headers and libraries.
This is usually done with the program "pkg-config". To get the compile flags, run:
pkg-config libwebsockets --cflags
To get the link flags:
pkg-config libwebsockets --libs
If you compile and link in the same step, you need to pass the flags that are output by both of the above commands to the compiler. If you have separate compilation and link commands, you pass the "--cflags" output during compiling, and the "--libs" output during linking.
In your case, you can compile with:
gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)
The $() syntax takes the output of the command you put between ( and ) and puts it into yours, as if you had typed it in.
You should probably write a small build script to do that for you. For example in a file named "build" in the same directory as "foo.c":
#! /bin/sh
gcc $(pkg-config libwebsockets --cflags) foo.c $(pkg-config libwebsockets --libs)
Make the script executable:
chmod +x build
And then just call it:
./build

Gtk-ERROR **: GTK+ 2.x symbols detected

I'm compiling my c application with gcc with the following flags:
gcc evis.c `pkg-config --cflags --libs gtk+-2.0 --libs clutter-gtk-1.0 --libs gthread-2.0` -Wall -o evis
Now my code compiles with a few warnings but still finishes. When I try to run my program I get:
(evis:1820): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
How do I troubleshoot this error? How do I know where to look? Is there some kind of tool I could use online that would scan for GTK3 symbols in my code? I'm compiling with GTK+2 so I don't understand how this is happening.
You are linking the same program to Gtk+2.0 and Gtk+3.0. And that will not work.
It is easy to check: just run the pkg-config command standalone. BTW, you do not need to repeat --libs so many times, and since we are looking for linking errors, I'm ommiting the --cflags for clarity:
$ pkg-config --libs gtk+-2.0 clutter-gtk-1.0 gthread-2.0
Now, it writes a lot of library names, but if you look carefully you'll find these ones:
... -lgtk-x11-2.0 ... -lgtk-3 ...
But where do they come from? Well, the Gtk+-2 part is easy: you are asking for it in the command line! The Gtk+-3 part has only one candidate:
$ pkg-config --libs clutter-gtk-1.0
... -lgtk-3 ...
Bingo! So Clutter-gtk is a Gtk+-3 library. And so should be your program is you want to use Clutter-gtk.
The solutions to your problem are:
Port your program to Gtk+-3 and change your compiler command accordingly.
Use a different version of Clutter-gtk that uses Gtk+-2. I think you can choose the dependency if you compile Clutter-gtk yourself.
Do not use Clutter-gtk.
I had the same issue while using matplotlib package in python. The below code solved the issue for me
import matplotlib
matplotlib.use('Agg')

Resources