当我尝试使用 Clion、CMake 和 Glade 使用以下配置运行 GTK3 时,我收到以下警告:
Could not find signal handler 'on_dumpButton_clicked'. Did you compile with -rdynamic?
所以 GTK 找不到我的任何信号处理程序。
CMAKE 设置:
cmake_minimum_required(VERSION 3.28)
project(pg_backer C)
set(CMAKE_C_STANDARD 11)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/glade" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
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(pg_backer main.c)
target_link_libraries(pg_backer PUBLIC
${GTK3_LIBRARIES}
)
我希望 Glade 构建器链接我的信号处理程序,但警告指出 GTK 无法找到它们,即使它们在我的代码和 Glade 文件中声明也是如此。
使用
-export-deynamic
标志更新 CmakeLists.txt 文件
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-export-dynamic")
您的 CMake 文件不应如下所示:
project(pg_backer C)
set(CMAKE_C_STANDARD 11)
#set(CMAKE_EXE_LINKER_FLAGS "-Wl,-export-dynamic")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/glade" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
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(pg_backer main.c)
target_link_libraries(pg_backer PUBLIC
${GTK3_LIBRARIES}
)
链接一些小部件并使用构建器获取林间空地文件并将其作为 GTK 对象返回的示例 C 代码:
#include <stdio.h>
#include <gtk/gtk.h>
GtkWidget *mainWindow;
GtkWidget *mainWindowFixedContainer;
GtkWidget *dumpButton;
GtkWidget *dumpLabel;
GtkBuilder *builder;
int main(int argc, char *argv[]) {
printf("Loading PG Backer...\n");
// Init GTK
gtk_init(&argc, &argv);
// Glade setup
builder = gtk_builder_new_from_file("glade/pg_backer.glade");
// build gtk widgets
mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow"));
mainWindowFixedContainer = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindowFixedContainer"));
dumpButton = GTK_WIDGET(gtk_builder_get_object(builder, "dumpButton"));
dumpLabel = GTK_WIDGET(gtk_builder_get_object(builder, "dumpLabel"));
// window
g_signal_connect(mainWindow, "destroy", G_CALLBACK(gtk_main_quit), NULL);
// signals
gtk_builder_connect_signals(builder, NULL);
gtk_widget_show(mainWindow);
// Run app
gtk_main();
return EXIT_SUCCESS;
}
void on_dumpButton_clicked(GtkButton *b)
{
gtk_label_set_text(GTK_LABEL(dumpLabel), (const gchar*)"Are you sure you want to dump X db from host?");
}