在前面帖子的安装了SDK,下面基于SDK创建图形界面应用程序。
创建工程目录
mkdir $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/stm32mp1-openstlinux-23.06.21
mkdir $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/stm32mp1-openstlinux-23.06.21/sources
创建程序目录
mkdir $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/stm32mp1-openstlinux-23.06.21/sources/gtk_hello_world_example
cd $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/stm32mp1-openstlinux-23.06.21/sources/gtk_hello_world_example
创建程序gtk_hello_world.c
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *button_box;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
gtk_container_add (GTK_CONTAINER (window), button_box);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_container_add (GTK_CONTAINER (button_box), button);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
创建Makefile脚本
PROG = gtk_hello_world
SRCS = gtk_hello_world.c
CLEANFILES = $(PROG)
# Add / change option in CFLAGS and LDFLAGS
CFLAGS += -Wall $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS += $(shell pkg-config --libs gtk+-3.0)
all: $(PROG)
$(PROG): $(SRCS)
$(CC) -o $[url=home.php?mod=space&uid=72445]@[/url] $^ $(CFLAGS) $(LDFLAGS)
clean:
rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))
交叉编译
网线连接板卡,复制可执行程序到板卡,其中192.168.1.4是板卡的IP。
scp gtk_hello_world root@192.168.1.4:/usr/local
在板卡上执行程序
cd /usr/local/
su -l weston -c "/usr/local/gtk_hello_world"
效果
点击按钮后,界面消失,终端打印Hello World
总结:ST提供的SDK十分完善,开发应用十分方便。
|