如何在 Windows 中更轻松地将 gtk 库与 cmake 链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I link gtk library more easily with cmake in windows?
提问by Gtker
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
):
我现在通过手动包含所有必需的路径(gtk 包位于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)
回答by liberforce
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.
2019-10-29 编辑:虽然这个答案仍然有效,但请注意,自 2011 年我的原始答案以来,CMake 已经发展了很多。对“现代 CMake”进行一些搜索,因为有许多语法和最佳实践的变化。
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.
只需将包含 pkg-config 的目录(在您的 gtk-bundle/bin 目录中)添加到您的 PATH 中。这样,CMake 就会找到它。
Here's a CMakeLists.txt for a sample application written in GTK2:
这是用 GTK2 编写的示例应用程序的 CMakeLists.txt:
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:
我的测试应用程序的 main.c 文件:
#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.
我在 Win XP 上使用 CMake 2.4 和 CMake 2.8 以及 MinGW 对其进行了测试,并且可以正常工作。它也应该在 MinGW 之外工作。
回答by Chen Levy
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.
我的 CMake 知识有些过时,我试图与 Windows 保持距离,因为我觉得它不愉快,但这就是FindGTK.cmake
应该的。
According to this poston the CMake mailing list you should:
根据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)
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.
更新:可能FindGTK.cmake
是 确实太旧了并且指的是 GTK1,您可能想尝试一下FindGTK2.cmake
。如果它不是您的 CMake 版本的一部分,您可以尝试从这里获取它。
Update2: Indeed the the FindGTK2 link above is no good for you. All it's logic is enclosed with:
Update2:确实上面的 FindGTK2 链接对您没有好处。它的所有逻辑都包含在:
if(UNIX)
...
endif(UNIX)
Update3: Again a quotefrom the mailing list:
更新 3:再次引用邮件列表中的内容:
... 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.zipavailable 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
... 这种传播到 Windows 的一个主要例子是 libGTK+ 和朋友(正如其他人在本主题中提到的那样)。一些 PLplot 驱动程序依赖于部分 GTK+ 库堆栈。我引用了我们的一位开发人员 (Werner Smekal) 的话,他最近向 PLplot 开发列表报告了让这些驱动程序在 Windows 上工作是多么简单:
Windows 中的 Cairo 驱动程序或在 Windows 上使用 gtk+ 进行 plplot
1) 下载 GTK+ 堆栈的多合一包,包括 Windows 的 3rd 方依赖项: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 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.
4) CMake 将找到 pkg-config 以及构建 pdfcairo、pscairo、pngcairo 和 svgcairo 设备所需的所有库。由于不存在 X-Header,因此不会构建 xcairo。
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%