Hi,
I tried and compiled a GTK 3 sample code in Ubuntu 18.04 OS and Code::Blocks IDE. Code is taken from this link (https://developer.gnome.org/gtk3/stable/gtk-getting-started.html). And for Code::Blocks setting I used this one (https://developer.gnome.org/gtk3/stable/gtk-compiling.html).
In "Compiler setting" , "Other compiler options" I used:
pkg-config --libs gtk+-3.0
In "Linker settings":
-pthread -lgtk-3 -lgdk-3 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0
In "Search directories" I added several but not all of above packages like:
/usr/include/
/usr/include/atk-1.0
And so on for about 8 include sources. Consequently I was able to "Build and run" my sample. A terminal was shown with a window which was expected. However I deleted first "Search directory" in order to removed all directories and build only from libraries. But now I get this error from Clang compiler:
With "Search directories":
-------------- Clean: Release in Temp (compiler: LLVM Clang Compiler 8)---------------
Cleaned "Temp - Release"
-------------- Build: Release in Temp (compiler: LLVM Clang Compiler 8)---------------
clang -m64 -pedantic -Weverything -Wall -std=c11 pkg-config --libs gtk+-3.0 -I/usr/include/ -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0/ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include/ -I/usr/include/pango-1.0/ -I/usr/include/cairo/ -I/usr/include/gdk-pixbuf-2.0/ -I/usr/include/atk-1.0 -c /home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c -o obj/Release/Main.o
clang: error: unsupported option '--libs'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'gtk+-3.0'
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Without "Search directories":
-------------- Clean: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
Cleaned "Temp - Release"
-------------- Build: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
clang -m64 -pedantic -Weverything -Wall -std=c11 pkg-config --libs gtk+-3.0 -c /home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c -o obj/Release/Main.o
clang: error: unsupported option '--libs'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'gtk+-3.0'
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Any help on this now ?
I haven't read your whole message, but the pkg-config --libs gtk+-3.0 command must be enclosed in backticks to work
`pkg-config --libs gtk+-3.0`
Above gives:
-------------- Clean: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
Cleaned "Temp - Release"
-------------- Build: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
clang -m64 -pedantic -Weverything -Wall -std=c11 -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -c /home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c -o obj/Release/Main.o
clang: warning: -lgtk-3: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lgdk-3: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lpangocairo-1.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lpango-1.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -latk-1.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcairo-gobject: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcairo: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lgdk_pixbuf-2.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lgio-2.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lgobject-2.0: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lglib-2.0: 'linker' input unused [-Wunused-command-line-argument]
/home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c:2:10: fatal error: 'gtk/gtk.h' file not found
#include <gtk/gtk.h>
^~~~~~~~~~~
1 error generated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 11 warning(s) (0 minute(s), 0 second(s))
But given "Search directories" as my original post, I was able to build. Lots of warning are generated and specially this takes some seconds to build. Can I just use libraries for every possible package and build faster than current one ?
In other words , can I build a static library from GTK 3 and link that to my project ?
File > New > Project > Static library
from :
/usr/include/gtk-3.0
I may download newer source from gtk site (https://www.gtk.org/download/linux.php) too.
Note that searching for gtk static library on my system I have :
:~$ mlocate *gtk*.a
/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.a
:~$
Lots of warning are generated
You are specifying -pedantic, -Weverything and -Wall, so don't surprise with the amount of warnings.
The 'linker' warnings are generated because you are compiling main.c to main.o, so no linking will be made, and you are specifying libraries. The pkg-config --libs part must be in the linker settings, not in the compiler settings. Linker settings should look similar to this (may be incomplete):
-pthread -lgthread -lrt `pkg-config --libs gtk+-3.0`
In order to build GTK3 as a library , I created a "Static library" project. Note that compilation usually takes about 20sec to 1min.
main.c : // for GTK3 project
void sample_function (void);
void sample_function (void)
{
return;
}
Here is the last part of Build log :
ar r bin/Release/libGTK3_SL.a obj/Release/main.o
ar: creating bin/Release/libGTK3_SL.a
Output file is bin/Release/libGTK3_SL.a with size 1.02 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
- Why static library file is about 1KB , is not that too small ?
Temp project Main.c :
#include "gtk/gtk.h"
//#include <gtk/gtk.h>
[...]
Error for Temp project calling GTK3 :
-------------- Clean: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
Cleaned "Temp - Release"
-------------- Build: Release in Temp (compiler: LLVM Clang Compiler 9 GTK)---------------
clang -m64 -pedantic -Weverything -Wall -std=c11 -c /home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c -o obj/Release/Main.o
/home/mehdi/Documents/Code_Blocks_Projects/Temp/Main.c:2:10: fatal error: 'gtk/gtk.h' file not found
#include "gtk/gtk.h"
^~~~~~~~~~~
1 error generated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
In "Linker setting" I have :
/home/mehdi/Documents/Code_Blocks_Projects/GTK3_SL/bin/Release/libGTK3_SL.a
- Why when I link to this library and I want to call gtk.h I receive an error ?
Now I am able to compile and show a window but why I get this error in "Build messages" :
error: Callback to invoke when an error occurs.