How to link gtk library more easily with cmake in windows?

Now I am doing this very ugly, manually, including all the necessary path (the gtk package is in D:/Tools/gtk+-bundle_2.20.0-20100406_win32):

include_directories(D:/Tools/gtk+-bundle_2.20.0-20100406_win32/include/gtk-2.0 D:/Tools/gtk+-bundle_2.20.0-20100406_win32/include/glib-2.0 D:/Tools/gtk+-bundle_2.20.0-20100406_win32/lib/glib-2.0/include D:/Tools/gtk+-bundle_2.20.0-20100406_win32/include/cairo D:/Tools/gtk+-bundle_2.20.0-20100406_win32/include/pango-1.0 D:/Tools/gtk+-bundle_2.20.0-20100406_win32/lib/gtk-2.0/include D:/Tools/gtk+-bundle_2.20.0-20100406_win32/include/atk-1.0)
link_directories(D:/Tools/gtk+-bundle_2.20.0-20100406_win32/lib)

target_link_libraries(MyProgram gtk-win32-2.0.lib)
+1
source share
2 answers

Just add the directory containing pkg-config (which is located in the gtk-bundle / bin directory) for your PATH. So CMake will find it.

Here's CMakeLists.txt for an example application written in GTK2:

cmake_minimum_required (VERSION 2.4)
project (gtk-test)

find_package (PkgConfig REQUIRED)
pkg_check_modules (GTK2 REQUIRED gtk+-2.0)

include_directories (${GTK2_INCLUDE_DIRS})
link_directories (${GTK2_LIBRARY_DIRS})
add_executable (gtk-test main.c)
add_definitions (${GTK2_CFLAGS_OTHER})
target_link_libraries (gtk-test ${GTK2_LIBRARIES})

And the main.c file for my test application:

#include <gtk/gtk.h>

int main (int argc, char **argv)
{
    GtkWidget *window;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Hello world !");
    g_signal_connect (G_OBJECT (window), "destroy", gtk_main_quit, NULL);

    gtk_widget_show_all (window);
    gtk_main ();

    return 0;
}

I tested it on Win XP with CMake 2.4 and CMake 2.8 and MinGW, and it works. It should also work outside MinGW.

+6
source

My CMake , Windows, , FindGTK.cmake.

CMake :

FIND_PACKAGE(GTK)

IF(GTK_FOUND)
   INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIR})
   ADD_EXECUTABLE(my_gtk_exe my_gtk_exe.cxx)
   TARGET_LINK_LIBRARIES(my_gtk_exe ${GTK_LIBRARIES})
ENDIF(GTK_FOUND)

. , FindGTK.cmake GTK1, FindGTK2.cmake. CMake, .

Update2: , FindGTK2 . :

if(UNIX)
  ...
endif(UNIX)

Update3: quote :

... Windows libGTK + ( ). PLplot GTK+. ( ), PLplot devel , :

Windows gtk + plplot Windows

     

1) "--" GTK +, :   http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.12/gtk+-bundle-2.12.9.zip   http://www.gtk.org/download-windows.html.

     

2) , . C:\Development\GTK

     

3) , CMake pkf-config

    set PKG_CONFIG_PATH=C:\Development\gtk\lib\pkgconfig
    set PATH=C:\Development\gtk\bin;%PATH%

4) CMake pkg-config ,   pdfcairo, pscairo, pngcairo svgcairo. xcairo    X- .

, :

set PKG_CONFIG_PATH=C:\path\to\gtk\lib\pkgconfig
set PATH=C:\path\to\gtk\bin;%PATH%
0

All Articles