windows下部署GTK环境
windows下部署GTK环境
1.下载MSYS2
下载地址:MSYS下载地址
选择:msys2-x86_64-20220904.exe
2.安装相关工具
# gcc
pacman -S mingw-w64-ucrt-x86_64-gcc
# gtk
pacman -S mingw-w64-x86_64-gtk4
# 语言开发包
pacman -S mingw-w64-x86_64-toolchain base-devel
注意:安装第二部时会出现错误
warning: too many errors from mirror.msys2.org, skipping for the remainder of this transaction
error: failed retrieving file 'mingw-w64-x86_64-gstreamer-1.22.1-1-any.pkg.tar.zst' from mirror.msys2.org : SSL connection timeout
error: failed retrieving file 'mingw-w64-x86_64-iso-codes-4.13.0-1-any.pkg.tar.zst' from repo.msys2.org : Resolving timed out after 10002 milliseconds
error: failed retrieving file 'mingw-w64-x86_64-gst-plugins-base-1.22.1-1-any.pkg.tar.zst' from repo.msys2.org : Connection time-out
error: failed retrieving file 'mingw-w64-x86_64-gsl-2.7.1-1-any.pkg.tar.zst' from repo.msys2.org : Connection time-out
warning: too many errors from repo.msys2.org, skipping for the remainder of this transaction
error: failed retrieving file 'mingw-w64-x86_64-gstreamer-1.22.1-1-any.pkg.tar.zst' from repo.msys2.org : Connection time-out
error: failed retrieving file 'mingw-w64-x86_64-gst-plugins-bad-libs-1.22.1-2-any.pkg.tar.zst' from repo.msys2.org : Connection time-out
error: failed retrieving file 'mingw-w64-x86_64-gsl-2.7.1-1-any.pkg.tar.zst.sig' from mirror.umd.edu : Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
warning: failed to retrieve some files
error: failed to commit transaction (unexpected error)
Errors occurred, no packages were upgraded.
原因时可能你的网络不好,连接超时,你可以反复执行第二个命令,每一次下载一些直到所有安装好。
3.创建环境变量
在PATH下增加:
${MYSY2 安装目录}\mingw64\bin
${MYSY2 安装目录}\mingw64\lib
${MYSY2 安装目录}\mingw64\include
4.验证
# PowerShell下输入
gcc --version
pkg-config --modversion gtk4
pkg-config --cflags --libs gtk4
出现即配置成功
5.CLion修改编译器
- 1.打开项目
- 2.选择“文件”—“设置”
- 3.选择“构建、执行、部署”
- 4.选择“工具链”
- 5.选择你下载的mingw
- 6.点击确定
6.编写CMake
这里我直接粘贴我的CMake文件,你可以自行参考。
不知道怎么修改可以参考我的上一篇文章:CMake链接第三方库
cmake_minimum_required(VERSION 3.25)
project(gtktest C)
set(CMAKE_C_STANDARD 11)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED gtk4)
include_directories(${GTK4_INCLUDE_DIRS})
link_directories(${GTK4_LIBRARY_DIRS})
add_executable(gtktest main.c)
target_link_libraries(gtktest ${GTK4_LIBRARIES})
7.编写测试代码
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello World");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
label = gtk_label_new("Hello, world!");
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
刚开始还不太懂,可以直接复制粘贴,运行结果会弹出一个窗口,从此我们告别黑白窗口。
声明:CSDN上讲GTK的文章比较少,安装都没有提,这是安装配置GTK环境最详细的步骤了!
欢迎大家阅读点赞。