本帖最后由 不爱胡萝卜的仓鼠 于 2023-12-11 17:01 编辑
今天来整一个HelloWorld,这下就必须要使用Ubuntu了,我用的一个虚拟机,Ubuntu 20.04
一、安装SDK
安装SDK在Ubuntu虚拟机中进行,首先安装额外的包
sudo apt-get update
sudo apt-get install gawk wget git diffstat unzip texinfo gcc-multilib chrpath socat cpio python3 python3-pip python3-pexpect
sudo apt-get install libssl-dev libgmp-dev libmpc-dev lz4 zstd
sudo apt-get install build-essential libncurses-dev libyaml-dev libssl-dev
sudo apt-get install coreutils bsdmainutils sed curl bc lrzsz corkscrew cvs subversion mercurial nfs-common nfs-kernel-server libarchive-zip-perl dos2unix texi2html libxml2-utils
额外的配置
echo 'options mmc_block perdev_minors=16' > /tmp/mmc_block.conf
sudo mv /tmp/mmc_block.conf /etc/modprobe.d/mmc_block.conf
下载SDK(SDK下载地址)
放到虚拟机中(在home路径下创建一个叫“STM32MPU_workspace”的文件夹,在这个文件夹下再创建一个名叫“tmp”的文件夹),解压
mkdir -p $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/SDK
修改SDK安装脚本的权限,使其可执行
chmod +x stm32mp1-openstlinux-6.1-yocto-mickledore-mp1-v23.06.21/sdk/st-image-weston-openstlinux-weston-stm32mp1-x86_64-toolchain-4.2.1-openstlinux-6.1-yocto-mickledore-mp1-v23.06.21.sh
执行SDK安装脚本
./stm32mp1-openstlinux-6.1-yocto-mickledore-mp1-v23.06.21/sdk/st-image-weston-openstlinux-weston-stm32mp1-x86_64-toolchain-4.2.1-openstlinux-6.1-yocto-mickledore-mp1-v23.06.21.sh -d /home/ubuntuadmin/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package/SDK
检查一下安装结果,在每次使用前都要source一下环境变量,如下
cd $HOME/STM32MPU_workspace/STM32MP1-Ecosystem-v5.0.0/Developer-Package
source SDK/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi
检查目标体系结构
echo $ARCH
检查toolchain
echo $CROSS_COMPILE
检查C编译器的版本
$CC --version
检查SDK版本
echo $OECORE_SDK_VERSION
二、HelloWorld
创建一个存放源代码的目录
创建一个存放helloworld代码的文件夹
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
然后创建两个文件,一个.c、一个Makeflie
然后在终端中直接输入make,完了有error,提示找不到gtk.h(图中路径请忽略,这是之前的截图)
那我得装一下gtk,这里特别注意,要装GTK3,不要装2.0(图中路径请忽略,这是之前的截图)
再次make,这次不报这个错误了,换别的了(图中路径请忽略,这是之前的截图)
不管了,听报错的,我改,改了2次,就可以编译通过了(图中路径请忽略,这是之前的截图)
得到的编译结果(图中路径请忽略,这是之前的截图)
把这个文件从虚拟机中拷贝到win,再通过SSH放到135的/usr/local/ 路径下
然后在终端中,输入命令运行helloworld
su -l weston -c "/usr/local/gtk_hello_world"
报错没有权限,看一下我对这个文件的权限
ls -l
修改一下权限
再次运行
完蛋,一堆乱码
经过一段时间的摸索,我找到了原因,还记得在SDK安装时有一句话吗?“在每次使用前都要source一下环境变量”,在打开一个终端后,要编译代码,就要先在终端source一下环境变量,然后在cd到代码路径,再make
终于,可以成功运行了
WeChat_20231210201440
最后贴一下.c和Makefile中的内容
#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_REPLACE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
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 $@ $^ $(CFLAGS) $(LDFLAGS)
clean:
rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS))