且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何交叉编译具有Yocto Linux库依赖关系的应用程序?

更新时间:2023-10-09 21:19:10

The Yocto eSDK allows you to cross-compile applications for your Yocto target on your PC using exactly the compiler and libraries used for the target itself. There is no need to add GCC to the target. The eSDK can be generated from a Yocto source tree, but since you have the source installed anyways, you don't need to install the eSDK and can build directly from the source. All of the this is not ARM specific but the general Yocto workflow to cross-compile for a specific Yocto target.

为此,请像往常一样先通过转入Yocto目录来设置Yocto构建环境,然后运行 source setup-environment yourbuilddir .

To do this, first setup the Yocto build environment as usual by changing into the Yocto directory, running source setup-environment yourbuilddir.

然后,运行 devtool add ,传递应用程序的名称(将用于BitBake配方)和现有源树的路径,例如:

Then, run devtool add, pass the name of your application (will be used for the BitBake recipe) and the path to the existing source tree, e.g.:

devtool add myhelloworld /home/user/Projects/myhelloworld

这将自动生成一个配方,并在您的Yocto源代码树中添加一个名为 workspace 的图层.该食谱将被命名为/yocto/source/path/yourbuilddir/workspace/recipes/myhelloworld/myhelloworld.bb .您可以通过运行

This will automatically generate a recipe and add a layer named workspace to your Yocto source tree. The recipe will be named something like /yocto/source/path/yourbuilddir/workspace/recipes/myhelloworld/myhelloworld.bb. You can edit it by running

devtool edit-recipe myhelloworld

使用 EDITOR = gedit 之类的命令为命令添加前缀,以使用您喜欢的文本编辑器.修改配方以正确构建您的应用程序;BitBake将自动确定是运行 make 还是 CMake .使用 make 的示例为:

Prefix the command with something like EDITOR=gedit to use your favourite text editor. Modify the recipe to build your application correctly; BitBake will automatically determine whether to run make or CMake. An example using make would be:

DESCRIPTION = "My GStreamer Hello World"
LICENSE = "CLOSED"
LIC_FILES_CHKSUM = ""

DEPENDS = "gstreamer1.0 pkgconfig-native"
FILES_${PN} += "${bindir}/myhelloworld ${datadir}/myhelloworld"

EXTRA_OEMAKE += "DESTDIR=${D}/ DATADIR=${datadir}/myhelloworld BINDIR=${bindir}"

do_install() {
    oe_runmake install
}

do_clean() {
    oe_runmake clean
}

DEPENDS 列出了相关性,我添加了 gstreamer1.0 以及 pkgconfig-native ,这在使用 pkg-config时是必需的 Makefile 中.如果需要,可以向 DEPENDS 添加更多依赖项,例如 boost openssl .合适的示例makefile是:

DEPENDS lists the dependencies, I added gstreamer1.0 and also pkgconfig-native which is neccessary when using pkg-config inside the Makefile. You can add more dependencies to DEPENDS if you need them, for example boost openssl. A suitable example makefile would be:

PACKAGES            = gstreamer-1.0

override CFLAGS     += `pkg-config --cflags $(PACKAGES)` -Wall -Wextra "-DDATADIR=\"$(DATADIR)/\"" -ffunction-sections -fdata-sections
override LIBS       += `pkg-config --libs $(PACKAGES)`
override LDFLAGS    += -Wl,--gc-sections

OBJS    = main.o
DEPS    = $(foreach file,$(OBJS),$(basename $(file)).d)
EXE     = myhelloworld

DESTDIR     ?=
PREFIX      ?=  $(HOME)/.local/
BINDIR      ?=  $(PREFIX)bin
DATADIR     ?=  $(PREFIX)share/$(PKGNAME)

.PHONY : clean install uninstall

all:    $(EXE)

$(EXE)  :   $(OBJS)
    $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

$(OBJS) : %.o   :   %.c %.d
    @[ -d "$(@D)" ] || mkdir -p "$(@D)"
    $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

$(DEPS) : %.d : $(PROJECT_ROOT)%.c
    @[ -d "$(@D)" ] || mkdir -p "$(@D)"
    $(CC) $(CFLAGS) $(CPPFLAGS) -M -MF $@ -MT "$(basename $@).o" $<

clean:
    rm -fr $(EXE) $(OBJS) $(DEPS)

install : $(EXE)
    install -DT -m 0755 $(EXE) $(DESTDIR)$(BINDIR)/$(EXE)

uninstall :
    rm -v $(DESTDIR)$(BINDIR)/$(EXE)

ifneq ($(MAKECMDGOALS), clean)
-include $(DEPS)
endif

在您的 Makefile 内,确保 make install 将应用程序二进制文件安装到 $(DESTDIR)$(BINDIR)/myhelloworld 和数据中文件(例如图像)到 $(DESTDIR)$(DATADIR)/somefile 中.在目标上,您的二进制文件将位于/usr/bin/myhelloworld 中,数据文件位于/usr/share/myhelloworld 中.在 Makefile 中,将-DDATADIR = \" $(DATADIR)/\" 传递给编译器,这样您就可以通过fopen(DATADIR"somepic.png","rb"); .对于源文件 main.c ,您可以使用

Inside your Makefile, make sure that make install installs your application binary into $(DESTDIR)$(BINDIR)/myhelloworld and data files (e.g. images) into $(DESTDIR)$(DATADIR)/somefile. On the target, your binary will then be located in /usr/bin/myhelloworld and the data files in /usr/share/myhelloworld. In the Makefile, pass "-DDATADIR=\"$(DATADIR)/\"" to the compiler such that you can open your data files by something like fopen(DATADIR "somepic.png", "rb");. For the source file main.c, you can use the GStreamer example 1.

保存配方, Makefile main.c ,然后运行

Save recipe, Makefile and main.c, and then run

devtool build myhelloworld

编译您的应用程序.如果一切顺利,您可以运行以下命令通过SSH将其安装到目标.

to compile your application. If all goes well, you can then install it to the target via SSH by running e.g.

devtool deploy-target myhelloworld root@targethostname

然后您可以通过键入 myhelloworld SSH到目标并运行您的应用程序.要卸载它:

You can then SSH to the target and run your application by typing myhelloworld. To uninstall it:

devtool undeploy-target myhelloworld root@targethostname

如果您以后决定将应用程序作为Yocto映像的一部分提供,请修改系统映像的配方并添加:

If you later decide to ship your application as part of the Yocto image, modify the recipe for your system image and add:

IMAGE_INSTALL_append = " yourhelloworld"

在第一引号后保留空格.构建映像时,将包括您的应用程序和数据.这种方法(使用BitBake配方)的主要优点是将应用程序添加到映像变得容易,并且该应用程序将与目标上可用的库完全链接,因此您可以完全使用这些版本中存在的功能

Keep the space after the first quote. When building the image, your application and data will be included. The main advantage of this approach (using a BitBake recipe) is that adding the application to the image becomes easy, and that the application will be linked against exactly the libraries available on the target, so you can use exactly the features present in these versions.

使用上面的示例makefile,您还可以通过简单地运行 make 来直接为您的主机PC编译.

Using the example makefile above, you can also directly compile for your host PC by simply running make.