Author Topic: GTK 3 in Ubuntu and Code::Blocks  (Read 504 times)

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
GTK 3 in Ubuntu and Code::Blocks
« on: October 31, 2019, 02:28:35 pm »
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. And for Code::Blocks setting I used this one.

In "Compiler setting"  , "Other compiler options" I used:
Code: [Select]
pkg-config --libs gtk+-3.0
In "Linker settings":
Code: [Select]
-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:
Code: [Select]
/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":
Code: [Select]

-------------- 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":

Code: [Select]

-------------- 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 ?

Offline Miguel Gimenez

  • Regular
  • ***
  • Posts: 369
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #1 on: October 31, 2019, 02:36:56 pm »
I haven't read your whole message, but the pkg-config --libs gtk+-3.0 command must be enclosed in backticks to work

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #2 on: October 31, 2019, 03:01:22 pm »
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:
Code: [Select]

-------------- 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 ?

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #3 on: October 31, 2019, 04:18:23 pm »
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 too.

Note that searching for gtk static library on my system I have :

Code: [Select]
:~$ mlocate *gtk*.a
/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.a
:~$

Offline Miguel Gimenez

  • Regular
  • ***
  • Posts: 369
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #4 on: October 31, 2019, 04:20:33 pm »
Quote
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):

Code: [Select]
-pthread -lgthread -lrt `pkg-config --libs gtk+-3.0`

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #5 on: November 01, 2019, 04:33:09 pm »
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
Code: [Select]
void sample_function (void);
void sample_function (void)
{
    return;
}

Here is the last part of Build log :

Code: [Select]
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 :

Code: [Select]
#include "gtk/gtk.h"
//#include <gtk/gtk.h>
[...]

Error for Temp project calling GTK3 :
Code: [Select]

-------------- 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 :

Code: [Select]
/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 ?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6664
    • My Best Post
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #6 on: November 01, 2019, 04:42:03 pm »
Quote
Code: [Select]
fatal error: 'gtk/gtk.h' file not found

You need to know the difference between linker and compiler errors!

What you posted was a Compiler Error; look in the Compiler setting.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #7 on: November 01, 2019, 04:54:41 pm »
Quote
Code: [Select]
fatal error: 'gtk/gtk.h' file not found

You need to know the difference between linker and compiler errors!

What you posted was a Compiler Error; look in the Compiler setting.

Tim S.

OK , I added `pkg-config --cflags gtk+-3.0` to "Compiler setting / Other compiler options" and now Temp compiles.

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 93
Re: GTK 3 in Ubuntu and Code::Blocks
« Reply #8 on: November 07, 2019, 03:00:37 pm »
Now I am able to compile and show a window but why I get this error in "Build messages" :

Code: [Select]
error: Callback to invoke when an error occurs.