Tlllune
Tlllune

Reputation: 11

Cannot compile Nim file using GTK+ 3

I 'm newbie.(And I cannot speak English better.)

I'm trying compile Nim code using gtk

{.push header:"<gtk/gtk.h>",varargs.}
proc gtk_init(argc,argv:pointer=nil)
proc gtk_window_new(typ:int):pointer
proc gtk_main_quit
proc gtk_widget_show(win:pointer)
proc gtk_main
{.pop.}
var maindow:pointer

gtk_init()
maindow=gtk_window_new(0)
maindow.gtk_widget_set_size_request(300,200)
maindow.gtk_widget_show()
gtk_main()

I'm using this command ->

nim c -r test

However,It couldn't succeed.

fatal error: gtk/gtk.h: No such file or directory
#include <gtk/gtk.h>
          ^~~~~~~~~~~
compilation terminated.

I already installed libgtk-3-dev ,but didn't solved the problem.

(so I don't know that syntax of the code is correct.)

What should I do for compile it?

Upvotes: 1

Views: 331

Answers (1)

zah
zah

Reputation: 5384

When you use GTK from a C program, you have to pass the correct include directories with -I and the correct linked libraries with -l. Usually, these are obtained by calling pkg-config:

$ pkg-config --cflags gtk+-3.0
-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/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/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include

$ pkg-config --libs gtk+-3.0                          
-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

Thus, to tell Nim to pass the right flags to the C compiler and linker, you can do the following:

{.passc: staticExec("pkg-config --cflags gtk+-3.0").}
{.passl: staticExec("pkg-config --libs gtk+-3.0").}

{.push header:"<gtk/gtk.h>",varargs.}
proc gtk_init(argc,argv:pointer=nil)
proc gtk_window_new(typ:int):pointer
proc gtk_main_quit
proc gtk_widget_show(win:pointer)
proc gtk_main
{.pop.}
var maindow:pointer

gtk_init()
...

Such usage of GTK wouldn't be very typical though. You may find it easeir to just use an existing wrapper such as nim-gtk3.

Upvotes: 2

Related Questions