3

I'm now doing it in a very ugly way by manually including all the required path(the gtk bundle is at 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)
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
Gtker
  • 2,237
  • 9
  • 29
  • 37

2 Answers2

18

2019-10-29 EDIT: While this answer could still work, please note that CMake has evolved a lot since my original answer in 2011. Do some search on "modern CMake" as there have been many syntactic and best practices changes.

Original Answer:

Just add the directory that contains pkg-config (which is in your gtk-bundle/bin directory) to your PATH. That way, CMake will find it.

Here's a CMakeLists.txt for a sample 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 app:

#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.

liberforce
  • 11,189
  • 37
  • 48
1

My CMake knowledge is somewhat dated, and I try to distance myself from Windows because I find it unpleasant, but this is what FindGTK.cmake should be for.

According to this post on the CMake mailing list you should:

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)

Update: It might be that the FindGTK.cmake is indeed too old and refers to GTK1, you might want to try FindGTK2.cmake instead. If it isn't part of your CMake version you can try and get it from here.

Update2: Indeed the the FindGTK2 link above is no good for you. All it's logic is enclosed with:

if(UNIX)
  ...
endif(UNIX)

Update3: Again a quote from the mailing list:

... One prime example of such spread to windows is libGTK+ and friends (as one other has mentioned in this thread). Some of the PLplot drivers depend on parts of the GTK+ stack of libraries. I quote from one of our developers (Werner Smekal) who recently reported to the PLplot devel list about how simple it is to get those drivers to work on windows:

Cairo driver in Windows or using gtk+ for plplot on Windows

1) Download the all-in-one bundle of the GTK+ stack including 3rd-party dependencies for windows: http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.12/gtk+-bundle-2.12.9.zip available from http://www.gtk.org/download-windows.html.

2) Expand the package to a directory, e.g. C:\Development\gtk

3) Set environment variables so that CMake can find pkf-config

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

4) CMake will find pkg-config and all the libraries necessary to build the pdfcairo, pscairo, pngcairo and svgcairo devices. xcairo will not be built since the X-Headers are not present.

So it seem you are missing the:

set PKG_CONFIG_PATH=C:\path\to\gtk\lib\pkgconfig
set PATH=C:\path\to\gtk\bin;%PATH%
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
  • I tried that before using the ugly one, it can't find gtk in windows. – Gtker Apr 28 '10 at 14:13
  • The only other thing I can think of, is to look at your FindGTK2.cmake, and see how is it try to find your GTK. Also compare your copy to the copy I linked to. If you find an error in the file, consider filing a bug about it. In my experience, the CMake people are approachable and helpful. – Chen Levy Apr 28 '10 at 14:37
  • I glanced through that file, it's only written for *nix environment. – Gtker Apr 28 '10 at 14:42
  • Tried setting the environment variables, and still it's not working.. Which seems certain since the FindGTK2 is written only for *nix platform. – Gtker Apr 28 '10 at 15:38
  • PkgConfig.cmake is the portable way to use any version of gtk, of course installed and registered in PATH correctly. – Joel May 12 '22 at 15:13