提示:以下是本篇文章正文内容,下面案例可供参考
原本是用vs2022配置的但....
# gcc
pacman -S mingw-w64-ucrt-x86_64-gcc
# gtk 4
pacman -S mingw-w64-x86_64-gtk4
# gtk 3
pacman -S mingw-w64-x86_64-gtk3
# 工具链
pacman -S mingw-w64-x86_64-toolchain base-devel
只安装gtk4时 CMakeLists.txt build 时报一些错误
注意顺序: 上面bin lib include 一定放在下面两个上面 不然后面用
pkg-config --modversion gtk4 验证的时候:Package gtk4 was not found in the pkg-config search path.
# PowerShell下输入
gcc --version
#gtk4 模块版本
pkg-config --modversion gtk4
#gtk3
pkg-config --modversion gtk+-3.0
#gtk3/4 库文件 4改3
pkg-config --cflags --libs gtk4
#demo
gtk4-demo
gtk3-demo
gtk4 (ProjectName字段改成你的项目名)
cmake_minimum_required(VERSION 3.2)
project(SimpleCalculatorPro)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK4 REQUIRED gtk4)
include_directories(${GTK4_INCLUDE_DIRS})
link_directories(${GTK4_LIBRARY_DIRS})
add_definitions(${GTK4_CFLAGS_OTHER})
add_executable(ProjectName main.cpp)
target_link_libraries(ProjectName ${GTK4_LIBRARIES})
gtk3 (ProjectName字段改成你的项目名)
cmake_minimum_required(VERSION 3.2)
project(SimpleCalculatorPro)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
add_executable(ProjectName main.cpp)
target_link_libraries(ProjectName ${GTK3_LIBRARIES})
#include <gtk/gtk.h>
static
void log_print()
{
g_print("this is a message\n");
}
static
void activate (
GtkApplication* app,
gpointer data
)
{
GtkWidget* window;
GtkWidget* button;
window = gtk_application_window_new(app);
button = gtk_button_new_with_label("hello world!");
g_signal_connect(button, "clicked", G_CALLBACK(log_print), nullptr);
gtk_window_set_default_size(GTK_WINDOW(window), 100, 300);
gtk_window_set_title(GTK_WINDOW(window), "test");
gtk_window_set_child(GTK_WINDOW(window), button);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc,
char** argv)
{
GtkApplication* app;
int status;
app = gtk_application_new("myapp.test.com", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), nullptr);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
}
pacman -S mingw-w64-x86_64-glade
------------------------------------- ½§¦ ‹ ‹‹‹…‹ 这是分割线中的分隔区 ›…››› ›¦§½ ----------------------------------------
https://www.gtk.org/docs/installations/windows/
https://www.gtk.org/docs/getting-started/hello-world/
https://developer-old.gnome.org/gtk4/
https://help.gnome.org/users/glade/stable/
https://developer.gnome.org/documentation/tutorials.html
https://packages.msys2.org/search?q=glade
我保留了GTK3模块,以防GTK4错误