如何在 Windows 中使用 cmake 更轻松地链接 gtk 库?
我现在通过手动包含所有必需的路径(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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2019-10-29 编辑:虽然这个答案仍然有效,但请注意,自 2011 年我最初的答案以来,CMake 已经发展了很多。对“现代 CMake”进行一些搜索,因为有许多语法和最佳实践更改.
原始答案:
只需将包含 pkg-config 的目录(位于您的 gtk-bundle/bin 目录中)添加到您的 PATH 中即可。这样,CMake 就会找到它。
下面是用 GTK2 编写的示例应用程序的 CMakeLists.txt:
以及我的测试应用程序的 main.c 文件:
我在 Win XP 上使用 CMake 2.4、CMake 2.8 和 MinGW 对其进行了测试,它可以工作。它也应该在 MinGW 之外工作。
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:
And the main.c file for my test app:
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.
我的 CMake 知识有些过时,我试图与 Windows 保持距离,因为我觉得它不愉快,但这就是
FindGTK.cmake
应该做的。根据 CMake 邮件列表上的这篇文章,您应该:
更新:可能是
FindGTK.cmake
确实太旧了并且引用了GTK1,您可能想尝试FindGTK2.cmake
。如果它不是您的 CMake 版本的一部分,您可以尝试从 这里。更新2:确实,上面的FindGTK2链接对您没有好处。它的所有逻辑都包含在:
Update3:再次引用来自邮件列表:
所以看来你错过了:
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:
Update: It might be that the
FindGTK.cmake
is indeed too old and refers to GTK1, you might want to tryFindGTK2.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:
Update3: Again a quote from the mailing list:
So it seem you are missing the: