How to include gtk/gtk.h in a C file - c

Im very new to development in C and would like to create a GUI using GTK. Ive already downloaded and installed gtk 3.6.4 bundle. Im trying to compile some example code like this:
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);
gtk_main();
return 0;
}
I'm really having trouble understanding how to include the header files. I've seen a couple threads which discuss using a pkg-config command like :
pkg-config --cflags --libs gtk+-3.0
Where should this code be inserted? It runs on the command window and generates some output but this doesnt solve the error I get in MS visual studio in my code. I apologize in advance for repeating a question but I still am having trouble understanding where this pkg-config code should be run and that is not clear to me from the other responses.

The pkg-config line is a shell command that produces on standard output the compiler flags you would need to pass to use the function you want. For example,
pkg-config --cflags gtk+-3.0
produces for me
-pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/mirclient -I/usr/include/mircommon -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng12 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
What you want to do is use a feature of your shell in which the output of a command is dropped into the command line of another one. In bash, this is done with `...`:
gcc -o program program.c `pkg-config --cflags --libs gtk+-3.0`
For bash, another syntax is $(...):
gcc -o program program.c $(pkg-config --cflags --libs gtk+-3.0)
For GNU make makefiles, if you want to use $(...), you will need to use the shell function:
gcc -o program program.c $(shell pkg-config --cflags --libs gtk+-3.0)
--cflags produces the C compiler flags, --libs produces the linker flags. If you're building using a makefile, you'll want to only provide --cflags to the recipe that produces the .o files and only provide --libs to the recipe that produces the final executable. For compiling a single C file directly into an executable with a single command, provide both.

Related

libSDL2_gfx-1.0.so.0: cannot open shared object file: No such file or directory

Creating an SDL2 application on Ubuntu 20.04 which uses the visual shape overlays provided by SDL2_gfx. Have both SDL2 and SDL2_gfx installed, I believe properly (./configure, make, sudo make install... all successful), compilation is success, but program execution is not.
Error message:
libSDL2_gfx-1.0.so.0: cannot open shared object file: No such file or directory
**Useful commands/results/information: **
user#debian$ pkg-config --cflags sdl2
-D_reentrant -I/usr/local/include -I/usr/local/include/SDL2
user#debian$ pkg-config --cflags SDL2_gfx
-D_reentrant -I/usr/local/include/SDL2 -I/usr/local/include -I/usr/local/include/SDL2
user#debian$ pkg-config --libs sdl2
-L/usr/local/lib -Wl, -rpath,/usr/local/lib -Wl, --enable-new-dtags -lSDL2
user#debian$ pkg-config --libs SDL2_gfx
-L/usr/local/lib -lSDL2_gfx -Wl, -rpath,/usr/local/lib -Wl, --enable-new-dtags -lSDL2
Compile argument: gcc sdlTest.c -o sdlTesting -I/usr/local/include -I/usr/local/include/SDL2
-L/usr/local/lib -L/usr/local/lib -lSDL2 -lSDL2_gfx
Also the compile argument:
gcc sdlTest.c -o sdlTesting -lSDL2 -lSDL2_gfx
...somehow works as well for compilation. Build error remains the same:
Tried the standard compilation using linker commands -lSDL2 -lSDL2_gfx, expected to have a normal execution using ./sdlTesting

Linking SDL in a C program

I have recently become interested in using SDL after having learned some basics of C. I have installed SDL_image and SDL_mixer. They are located in /usr/local/include/SDL2. I realize that you must link against the header files however I am not sure how to do it. I am getting the error that SDL_mixer or SDL_image do not exist (depending on their line order in my source code). I have tried two different compilation commands and neither work here they are:
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
gcc filename.c -o test -I./usr/local/include/SDL2 -L./lib -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
If anyone has any ideas I would appreciate it! Thanks in advance!
you do not want that leading period
wrong
gcc filename.c -o test -I./include -L./usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
closer - not necessarily correct yet
gcc filename.c -o test -L/usr/local/include/SDL2 -lSDL2main -lSDL2 -lSDL_mixer -lSDL_image
any path with a leading period indicates to start from current dir and go relative instead of the intended absolute path
any system has the notion of a default library path which is fine if you are using a standard install ... so no need to do a
-I/include
... sometime a library has helpers to identify and auto populate these ...
sdl and sdl2 do have such a helper ... this will give you those settings
gcc -o test filename.c `pkg-config --cflags --libs sdl2`
notice those backticks ... another syntax style would be
gcc -o test filename.c $(pkg-config --cflags --libs sdl2)
you are free to issue that stand alone just to take a peek
pkg-config --cflags --libs sdl2
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2
Now onto your sdl mixer ... well it has a
pkg-config --cflags --libs SDL2_mixer
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_mixer -lSDL2
you probably do not want to mix sdl with sdl2 so replace mention of
-lSDL_mixer -lSDL_image
with
-lSDL2_mixer -lSDL2_image
as per
pkg-config --cflags --libs SDL2_image
... output
-D_REENTRANT -I/usr/include/SDL2 -lSDL2_image -lSDL2
so bundling these together
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2) $(pkg-config --cflags --libs SDL2_mixer) $(pkg-config --cflags --libs SDL2_image)
or more simply combined to
gcc -o test filename.c -lSDL2main $(pkg-config --cflags --libs sdl2 SDL2_mixer SDL2_image )
this can be stripped down to simply the following ... yet above syntax is more robust to changes
gcc -o test filename.c -D_REENTRANT -I/usr/include/SDL2 -lSDL2main -lSDL2 -lSDL2_mixer -lSDL2_image
You can use sdl2-config to supply the appropriate flags to gcc:
gcc filename.c -o test `sdl2-config --cflags --libs`
sdl2-config --cflags produces a list of options that should be passed to the compiler, and sdl2-config --libs produces a list of libraries that should be linked to.

Signal in Glade GTK+3 do not work

I am trying to learn how to program a GTK application. I created my first application and here is the callback function:
void click_button2 (GtkToggleButton *tbutton, gpointer data)
{
gtk_main_quit ();
}
And here is how my GTK project looks: http://i.imgur.com/FR58rhT.png
After compiling with: gcc -Wall -g -o testGlade test.c pkg-config --cflags --libs gtk+-3.0 gmodule-2.0
The cal back does not seem to work. Nothing happens when I click the button.
Please add your source code of the test.c for further help.
Try compiling with -rdynamic option.
gcc -Wall -g -o testGlade test.c pkg-config --cflags --libs gtk+-3.0 gmodule-2.0 -rdynamic

glibconfig.h no such file or directory

I just installed glib in Raspbian(Debian version). I want to read a config file using glib. I am trying to write a C application in Codeblocks and I use the header
#include <glib.h>
But I have an error in gtypes.h
fatal error:glibconfig.h No such file or directory
I used this path
project->Build Options->Compiler Settings->Other Options
and I added
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include
as I read in this tutorial. I have to declare also
-L/usr/lib -lm -lglib-2.0
as the tutorial says and if yes then where and how can I declare it in Codeblocks?
Use pkg-config.
gcc `pkg-config --cflags glib-2.0` foo.c `pkg-config --libs glib-2.0`

how to compile a program with gtkmozembed.h

i have written a program under ubuntu, in which i include gtkmozembed.h. I am facing a problem in compiling the program.Below is the simplest form of a program which uses gtkmozembed.
#include <gtk/gtk.h>
#include <stdio.h>
#include <gtkmozembed.h>
int main(){
GtkWidget *mozEmbed;
mozEmbed = gtk_moz_embed_new();
return 0;
}
Eventhough, the above program is doing nothing, compiling that program is a lot for me...
I am trying to comile the above program like below
gcc `pkg-config --libs --cflags gtk+-2.0` test.c -o test
and it is giving the following error...
error: gtkmozembed.h: No such file or directory
I can understand, something else has to be added to the above gcc line,so that the compiler can find the gtkmozembed.h, but not getting what is that, 'something'...Looking for someone's help..Thank you...
Install libxul-dev (sudo apt-get install libxul-dev) and include
#include <gtkmozembed.h>
in the main file(test.c) and compile with
gcc `pkg-config --cflags --libs gtk+-2.0 xulrunner-gtkmozembed` test.c -o test
Your problem is that gtkmozembed.h is not found in the standard include file lookup path (well, the error does tell you that pretty obviously). On my system it lives in $(include)/gtkmozembed/, so you have two options
Change the path of the included file in your source
#include <gtkmozembed/gtkmozembed.h>
or manually add the path to the lookup path
gcc `pkg-config --libs --cflags gtk+-2.0` -I/usr/include/gtkmozembed test.c -o test
You should go with option 1).
This will tell gcc where to find the include file, but as pointed out by Matthew this is not enough: you will most probably also need to add more information for linking and required additional includes. Thankfully gtk-mozembed comes with a pkg-config file, so you can get all the needed information like you did for gtk+-2.0 with
pkg-config --libs --cflags mozilla-gtkmozembed-embedding
or combined with the other call
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test
You should also (just for kicks) have a look at what pkg-config does. The part in "`" is just what is return by the shell when executing that command. On my machine:
$ pkg-config --libs --cflags mozilla-gtkmozembed-embedding
-DXPCOM_GLUE -fshort-wchar \
-I/usr/include/xulrunner-1.9.2 -L/usr/lib/xulrunner-devel-1.9.2/lib -lxpcomglue
(line breaks added by me). The -I parts just adds additional needed directories to the include file lookup path -- they were emitted because you called with --cflags. The entries with -lxpcomglue is due to calling with --libs and ask for linking against this library, i.e. libxpcomglue.so. It is located in /usr/lib/xulrunner-devel-1.9.2/lib. The rest are a define and a gcc flag needed for gtkmozembed.
Try this:
gcc `pkg-config --libs --cflags gtk+-2.0 mozilla-gtkmozembed-embedding` test.c -o test

Resources