Yocto: Hello World!

本文详细介绍了如何在Yocto项目中添加自定义包,包括创建新的核心镜像配方、编写简单的C语言程序、制作Bitbake配方文件进行交叉编译等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This blog describes in detail how to add a custom package into the Yocto project.

Step 1:

In order to add a package, you've got to let the bitbake know that which package's being added. There' are several ways to do this, of course. But I'll just show you the simplest way.

Copy the meta/recipes-sato/images/core-image-sato.bb to a new .bb (e.g. my-core-image-sato.bb) and add the following line to the end of the copy:

IMAGE_INSTALL += "helloworld"

Step 2:

Making a hello.c file with the following content:

#include "stdio.h"

void main() {
    printf("hello, world\n");
}

Step 3:

Create a helloworld_0.0.bb Bitbake recipe file which instructs Bitbake to cross-compile and package our hello world program.

Make a new directory under Poky, say, meta-custom, and put the recipe file there, together with the hello.c file. For simplicity, just copy the conf directory under meta to the meta-custom, make a new dir called recipes-demo and put the .bb file there, as well as the .c file.

Here's what the .bb file should look like:

DESCRIPTION = "Hello World sample program"
PR = "r0"
SRC_URI = "file://hello.c"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://hello.c;md5=a037431be1bc2056740c02eb59718e3f"
S = "${WORKDIR}"

do_compile() {
             ${CC} ${CFLAGS} ${LDFLAGS} -o hello hello.c
}

do_install() {
             install -d ${D}${bindir}/
             install -m 0755 ${S}/hello ${D}${bindir}/
}

FILES_${PN} = "${bindir}/hello"

For the md5 checksum, use the command "md5sum hello.c" to generate it.

step 4:

Add a new line the the bblayer.conf file.

BBLAYERS ?= " \
  /home/yocto/poky-denzil-7.0.1/meta \
  /home/yocto/poky-denzil-7.0.1/meta-yocto \
  /home/yocto/poky-denzil-7.0.1/meta-skeleton \
  /home/yocto/poky-denzil-7.0.1/meta-custom \
  "

In this way, BitBake knows where to search the .bb files.

step 5:

bitbake -k my-core-image-sato

step 6:

runqemu qemux86

DONE!

 



 

转载于:https://my.oschina.net/u/158589/blog/72589

<think>嗯,用户想了解如何使用Yocto默认环境从零开始编译一个Hello World程序。首先,我需要回想一下Yocto的基本概念和工作流程。Yocto项目是一个用于构建定制Linux系统的工具,它使用层(layers)来组织配置和配方(recipes)。用户可能对Yocto有一定的了解,但可能还不清楚如何创建自己的recipe来编译一个简单的程序。 首先,用户可能需要一个基本的Yocto环境设置。我应该先介绍如何安装必要的依赖,克隆Yocto的仓库,比如Poky,然后初始化构建环境。然后,他们需要创建自己的层(layer)来存放自定义的recipe。这可能涉及到使用bitbake-layer命令创建新层,并添加到bblayers.conf中。 接下来,编写Hello World的C程序。用户可能知道如何写这个程序,但需要将其放在适当的目录结构中,比如files文件夹下。然后,需要创建一个recipe文件(.bb),其中指定源码位置、编译指令和安装步骤。这里可能需要解释如何编写基本的do_compile和do_install任务,确保生成的二进制文件被正确安装到目标目录。 然后,用户需要将层添加到构建配置中,并使用bitbake命令来构建recipe。可能还需要说明如何将生成的镜像包含进系统镜像中,或者单独构建recipe。最后,测试编译好的程序,可能是在QEMU模拟器中运行,或者部署到实际硬件,并运行Hello World程序。 用户可能会有一些疑问,比如如何处理依赖关系,或者如何调试构建过程中的错误。我应该提到如何检查日志文件,比如temp/log.do_compile,来排查问题。另外,可能需要提醒用户确保层配置正确,文件路径无误,以及编译任务中的命令正确使用交叉编译工具链。 另外,用户可能希望流程尽可能详细,所以需要分步骤说明,确保每个命令和文件路径都清晰。可能还需要强调Yocto的工作目录结构,比如工作目录(build)、源文件存放位置(recipes-example/hello/files/),以及层的位置。 总结一下,回答的结构应该是:环境准备、创建层、编写程序、创建recipe、构建和测试,以及常见问题处理。需要确保用户能够按照步骤一步步操作,并理解每个步骤的作用。同时,要使用简单的中文,避免过于技术性的术语,或者在必要时解释术语。</think>好的,以下是使用Yocto从零开始编译一个Hello World程序的详细步骤: --- ### **1. 环境准备** #### 1.1 安装依赖 ```bash sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat cpio python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev pylint xterm ``` #### 1.2 获取Yocto源码(以Poky为例) ```bash git clone git://git.yoctoproject.org/poky cd poky git checkout <指定版本,如:kirkstone> # 可选,建议使用稳定版本 ``` #### 1.3 初始化构建目录 ```bash source oe-init-build-env build ``` 这会创建并进入 `build` 目录,生成默认配置文件 `conf/local.conf` 和 `bblayers.conf`。 --- ### **2. 创建自定义层(Layer)** #### 2.1 生成新层 ```bash bitbake-layers create-layer ../meta-hello ``` 这会生成一个名为 `meta-hello` 的新层目录。 #### 2.2 添加层到构建配置 修改 `build/conf/bblayers.conf`,在 `BBLAYERS` 中添加新层路径: ```bash BBLAYERS += " ${TOPDIR}/../meta-hello" ``` --- ### **3. 编写Hello World程序** #### 3.1 创建源码文件 在层目录中新建源码文件夹: ```bash mkdir -p meta-hello/recipes-example/hello/files ``` 在 `files` 目录下创建 `hello.c`: ```c #include <stdio.h> int main() { printf("Hello, Yocto World!\n"); return 0; } ``` #### 3.2 创建编译脚本(可选) 如果项目需要复杂编译步骤,可以在 `files` 下创建 `Makefile`: ```makefile CC = ${CC} CFLAGS = -Wall all: hello hello: hello.c $(CC) $(CFLAGS) -o $@ $^ clean: rm -f hello ``` --- ### **4. 编写Recipe文件** 在 `meta-hello/recipes-example/hello` 目录下创建 `hello_0.1.bb`: ```bitbake SUMMARY = "Simple Hello World program" LICENSE = "MIT" LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" SRC_URI = "file://hello.c \ file://Makefile" S = "${WORKDIR}" do_compile() { oe_runmake } do_install() { install -d ${D}${bindir} install -m 0755 hello ${D}${bindir} } ``` --- ### **5. 编译程序** #### 5.1 执行构建 ```bash bitbake hello ``` Yocto会自动下载依赖、编译源码,并将二进制文件打包到目标系统镜像中。 #### 5.2 查找输出文件 编译完成后,生成的二进制文件位于: ```bash build/tmp/work/<架构>/hello/0.1-r0/image/usr/bin/hello ``` (例如,x86_64架构路径为 `tmp/work/x86_64-poky-linux/hello/0.1-r0/image/usr/bin/hello`) --- ### **6. 测试程序** #### 方法一:添加到系统镜像 修改 `conf/local.conf`,添加: ```bash IMAGE_INSTALL:append = " hello" ``` 然后编译完整镜像: ```bash bitbake core-image-minimal ``` 在QEMU或硬件上启动镜像后,运行 `hello` 即可看到输出。 #### 方法二:直接运行二进制 从输出目录直接拷贝二进制文件到目标设备运行: ```bash ./hello # 输出: Hello, Yocto World! ``` --- ### **常见问题** 1. **编译失败** 检查日志文件:`tmp/work/<架构>/hello/0.1-r0/temp/log.do_compile`。 2. **Recipe找不到源码** 确保 `SRC_URI` 中的文件路径正确,且源码位于 `files` 目录。 3. **交叉编译问题** Yocto自动使用目标架构的工具链,无需手动指定编译器。 --- 通过以上步骤,你可以在Yocto环境中完成从源码到二进制程序的完整构建流程。如果需要更复杂的配置(如依赖其他库),可以在Recipe中添加 `DEPENDS` 字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值