c - 如何在 yocto 中编译一个基本的 c 文件

我正在研究 yocto,我想在 yocto 中编译一些 C 文件并将生成的二进制文件安装到外部文件系统。 在这样做之前,我尝试创建一个单独的 reciepe 并从中编译 c 代码。 我无法编译它。

最佳答案

我不确定这个问题是否理解,因为它不够精确。

在配方树中包含 C 文件

如果你想在你的食谱中有 C 文件,有一个像这样的文件树:

recipe-example/example/example_0.1.bb
recipe-example/example/example-0.1/helloworld.c

您可以在使用创建新图层时生成此示例

yocto-layer <your-layer-name>

您的 bb 文件将如下所示:

#
# This file was derived from the 'Hello World!' example recipe in the
# Yocto Project Development Manual.
#
SUMMARY = "Simple helloworld application"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://helloworld.c"

S = "${WORKDIR}"

do_compile() {
         ${CC} helloworld.c -o helloworld
}

do_install() {
         install -d ${D}${bindir}
         install -m 0755 helloworld ${D}${bindir}
}

它将编译 hello world 文件并将其安装到镜像上的/usr/bin 中。

来自 Git 仓库

您也可以从 git 存储库编译,我建议您阅读 yocto 文件夹中的手册和示例。这是 wiringPi 的示例:

DESCRIPTION = "A library to control Raspberry Pi GPIO channels"
HOMEPAGE = "https://projects.drogon.net/raspberry-pi/wiringpi/"
SECTION = "devel/libs"
LICENSE = "LGPLv3+"
LIC_FILES_CHKSUM = "file://COPYING.LESSER;md5=e6a600fd5e1d9cbde2d983680233ad02"

# tag 2.29
SRCREV = "d79506694d7ba1c3da865d095238289d6175057d"

S = "${WORKDIR}/git"

SRC_URI = "git://git.drogon.net/wiringPi \
           file://0001-Add-initial-cross-compile-support.patch \
           file://0001-include-asm-ioctl.h-directly-for-_IOC_SIZEBITS.patch \
           "

COMPATIBLE_MACHINE = "raspberrypi"

CFLAGS_prepend = "-I${S}/wiringPi -I${S}/devLib"

EXTRA_OEMAKE += "'INCLUDE_DIR=${D}${includedir}' 'LIB_DIR=${D}${libdir}'"
EXTRA_OEMAKE += "'DESTDIR=${D}/usr' 'PREFIX=""'"

do_compile() {
    oe_runmake -C devLib
    oe_runmake -C wiringPi
    oe_runmake -C gpio 'LDFLAGS=${LDFLAGS} -L${S}/wiringPi -L${S}/devLib'
}

do_install() {
    oe_runmake -C devLib install
    oe_runmake -C wiringPi install
    oe_runmake -C gpio install
}

它从 git 存储库中获取,应用 git 生成的补丁,使用 oe_runmake 与 makefile 一起编译。

使用开发工具

有人在评论中询问如何使用 devtool 添加配方。 我们还是会再次以 wiringPi 为例。下载它做 https://github.com/WiringPi/WiringPi Makefile 是文件夹 wiringPi。 然后你可以做

devtool add <name_of_recipe> <path_to_Makefile_folder>

注意devtool的警告

NOTE: Creating workspace layer in /home/dbensoussan/new_poky/poky/build/workspace
NOTE: Enabling workspace layer in bblayers.conf
NOTE: Using source tree as build directory since that would be the default for this recipe
NOTE: Recipe /home/dbensoussan/new_poky/poky/build/workspace/recipes/project/project.bb has been automatically created; further editing may be required to make it fully functional

这是生成如下配方:

# Recipe created by recipetool
# This is the basis of a recipe and may need further editing in order to be fully functional.
# (Feel free to remove these comments when editing.)
#
# WARNING: the following LICENSE and LIC_FILES_CHKSUM values are best guesses - it is
# your responsibility to verify that the values are complete and correct.
LICENSE = "Unknown"
LIC_FILES_CHKSUM = "file://COPYING.LESSER;md5=e6a600fd5e1d9cbde2d983680233ad02"

# No information for SRC_URI yet (only an external source tree was specified)
SRC_URI = ""


# NOTE: this is a Makefile-only piece of software, so we cannot generate much of the
# recipe automatically - you will need to examine the Makefile yourself and ensure
# that the appropriate arguments are passed in.

do_configure () {
    # Specify any needed configure commands here
    :
}

do_compile () {
    # You will almost certainly need to add additional arguments here
    oe_runmake
}

do_install () {
    # This is a guess; additional arguments may be required
    oe_runmake install 'DESTDIR=${D}'
}

然后您可以编辑您的配方以适合您的配置

使用外部资源

可以通过使用 externalsrc 使用文件系统上的目录。 我自己没有尝试过,也没有准备好工作区,但是评论中的@71GA 测试了 Koan 软件公司的教程 https://wiki.koansoftware.com/index.php/Building_Software_from_an_External_Source它奏效了。我将内容复制到这里:

在这种情况下使用 externalsrc 类 - 您可以在原始 bb 配方或 bbappend 中继承它:

inherit externalsrc
EXTERNALSRC = "/path/to/sources"

根据构建类型(例如,Linux 内核模块之外的“继承模块”),您可能需要也可能不需要设置 EXTERNALSRC_BUILD。

inherit externalsrc
EXTERNALSRC = "/some/path"
EXTERNALSRC_BUILD = "/some/path"

如果您要在多个食谱中使用它,您可以在配置级别全局继承它(可能通过您在其中包含/需要的 inc 文件):

INHERIT += "externalsrc"
EXTERNALSRC_pn-<recipename> = "/path/to/sources"

使用 nInvaders 包的外部源的配方示例

#
# Recipe example with externalsrc
#
# (C)2019 Marco Cavallini - KOAN - <https://koansoftware.com>
#

LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""

inherit externalsrc

EXTERNALSRC = "/home/koan/yocto-qemuarm-sumo/ninvaders-0.1.1"
EXTERNALSRC_BUILD = "${EXTERNALSRC}"

DEPENDS = "ncurses"

EXTRA_OEMAKE = "-e"


do_install() {
    install -d ${D}${bindir}
    install -m 0755 nInvaders ${D}${bindir}
}

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

https://stackoverflow.com/questions/37705995/

相关文章:

python - 如何将 __repr__ 与多个参数一起使用?

ios - Firebase 未正确初始化

php - 无法移 Action 曲家

objective-c - 比较 objective-C 中的 2 个字符串

javascript - 如何更改 react js中的默认端口

macos - Applescript 制作新文件夹

python - 如何获取和使用 Dropbox API 的刷新 token (Python 3.x

vb.net - 如何拆分逗号分隔的字符串?

java - 为什么我在 Java 中对 char 和 int 进行比较不起作用?

r - 在 R 中转换日期格式