Yocto / Poky:在单独的层上安装和使用共享库 .so

Yocto / Poky: install and use shared library .so on separate layers

我正在尝试学习如何使用 Yocto 构建自定义 linux 图像,并且我正在努力创建同时具有共享库和使用它的程序的图像。

我开始关注 this tutorial,一切顺利。然后我尝试在两个不同的层中将程序和库分开,但没有成功。

我从库代码入手:

greetings.c

#include <stdio.h>
#include <string.h>
#include "greetings.h"
void get_greeting(char * buffer) {

    if(buffer == NULL) {
        return;
    }

    char greeting[] = "Hello world from the greetings lib\n";
    strcpy(buffer, greeting);
    return;
}

greetings.h

void get_greeting(char * buffer);

Makefile.am

AUTOMAKE_OPTIONS = foreign

lib_LTLIBRARIES = libgreetings.la
libgreetings_la_SOURCES = greetings.c

include_HEADERS = greetings.h
libgreetings_la_CFLAGS = -Wall -Werror -fPIC
libgreetings_la_LDFLAGS = -shared
ACLOCAL_AMFLAGS = -I m4

configure.ac

AC_INIT([Greetings lib], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_MACRO_DIR([m4])
LT_INIT()
AC_CONFIG_FILES(Makefile)
AC_OUTPUT

我将此代码添加到 git 存储库并使用 layer.conf 和配方文件创建了一个 "meta-greetings" 层:

layer.conf

# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"

# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
            ${LAYERDIR}/recipes-*/*/*.bbappend"

BBFILE_COLLECTIONS += "meta-greetings"
BBFILE_PATTERN_meta-greetings = "^${LAYERDIR}/"
BBFILE_PRIORITY_meta-greetings = "6"

LAYERDEPENDS_meta-greetings = "core"
LAYERSERIES_COMPAT_meta-greetings = "thud"
IMAGE_INSTALL_append = " greetings"

recipes-greetings/greetings/greetings_0.1.bb

SUMMARY = "bitbake-layers recipe"
DESCRIPTION = "Simple helloworld lib"
DEPENDS = ""
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=96af5705d6f64a88e035781ef00e98a8"

KBRANCH = "master"
SRCREV = "1a908a8f8616af704ce71d693e88c6d4498f24c4"

SRC_URI = "git://bitbucket.org/Grifo/greetings_lib.git;branch=${KBRANCH};protocol=ssh"

S = "${WORKDIR}/git"

inherit autotools

到目前为止一切顺利,我将这一层添加到我的 bblayers 文件中并继续编译最终图像。我 运行 它在 qemu 中,甚至可以看到 /usr/lib:

中的文件

然而,没有 "libgreetings.so"。我不知道这是否可能是问题的原因(仍有待解释),但前面提到的教程得到了类似的结果,所以我继续。

之后我做了程序:

helloworld.c

#include <stdio.h>
#include "greetings.h"
int main(void) {
    char greeting[40];
    get_greeting(greeting);
    printf("Hello world!\n");
    printf("%s", greeting);
    return 0;
}

Makefile.am

AUTOMAKE_OPTIONS = foreign

bin_PROGRAMS = hello_world
hello_world_SOURCES = helloworld.c
hello_world_LDADD = $(libdir)/libgreetings.so
ACLOCAL_AMFLAGS = -I m4

configure.ac

AC_INIT([Hello world], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_FILES(Makefile)
AC_OUTPUT

将此代码添加到 git 并使用以下文件创建了一个 "meta-helloworld" 层:

layer.conf

# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"

# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
            ${LAYERDIR}/recipes-*/*/*.bbappend"

BBFILE_COLLECTIONS += "meta-helloworld"
BBFILE_PATTERN_meta-helloworld = "^${LAYERDIR}/"
BBFILE_PRIORITY_meta-helloworld = "7"

LAYERDEPENDS_meta-helloworld = "core meta-greetings"
LAYERSERIES_COMPAT_meta-helloworld = "thud"
IMAGE_INSTALL_append = " helloworld"

recipes-helloworld/helloworld/helloworld_0.1.bb

SUMMARY = "bitbake-layers helloworld"
DESCRIPTION = "Simple helloworld program"

LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=96af5705d6f64a88e035781ef00e98a8"

KBRANCH = "master"
SRCREV = "6a29425473286028e85e74003f2f57ecaf766354"

SRC_URI = "git://bitbucket.org/Grifo/hello_world.git;branch=${KBRANCH};protocol=ssh"

DEPENDS = "greetings"

S = "${WORKDIR}/git"

inherit autotools

在这一切之后,我再次对最终图像进行了 bitbaked,但出现了以下错误:

(...)
Sstate summary: Wanted 7 Found 0 Missed 7 Current 737 (0% match, 99% complete)
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
ERROR: helloworld-0.1-r0 do_compile: oe_runmake failed
ERROR: helloworld-0.1-r0 do_compile: Function failed: do_compile (log file is located at /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040)
ERROR: Logfile of failure stored in: /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040
Log data follows:
| DEBUG: SITE files ['endian-little', 'bit-32', 'arm-common', 'arm-32', 'common-linux', 'common-glibc', 'arm-linux', 'arm-linux-gnueabi', 'common']
| DEBUG: Executing shell function do_compile
| NOTE: make -j 8
| make: *** No rule to make target `/usr/lib/libgreetings.so', needed by `hello_world'.  Stop.
| ERROR: oe_runmake failed
| WARNING: /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/run.do_compile.12040:1 exit 1 from 'exit 1'
| ERROR: Function failed: do_compile (log file is located at /var/tmp/workspaces/grifo/poky/build/tmp/work/armv5e-poky-linux-gnueabi/helloworld/0.1-r0/temp/log.do_compile.12040)
ERROR: Task (/var/tmp/workspaces/grifo/poky/meta-helloworld/recipes-helloworld/helloworld/helloworld_0.1.bb:do_compile) failed with exit code '1'
NOTE: Tasks Summary: Attempted 1966 tasks of which 1965 didn't need to be rerun and 1 failed.

Summary: 1 task failed:
  /var/tmp/workspaces/grifo/poky/meta-helloworld/recipes-helloworld/helloworld/helloworld_0.1.bb:do_compile
Summary: There was 1 WARNING message shown.
Summary: There were 2 ERROR messages shown, returning a non-zero exit code.

很抱歉这个问题很长,但我觉得我需要提供所有详细信息,因为我不知道问题是出自我的食谱还是我的 autotools 文件。

在我构建食谱并使用 yocto 编译它之前,我首先使用 shell 在我的主机中编译并 运行 它并且一切 运行 都很好。我编译并 make install 问候语库 (/usr/local/lib),然后编译 运行 的 helloworld 程序没有任何问题。

我知道我可能可以在同一层中轻松地完成所有这些操作,但是我正在尝试在不同的层中进行操作以模拟不同的项目。我的另一个要求是使用 autotools 而不是 cmake。

提前谢谢你,

Grifo

编辑:

我成功了!谢谢 Alexander Kanavin 为我指明了正确的方向。我只需要在 helloworld 的 Makefile.am.

中将 hello_world_LDADD = $(libdir)/libgreetings.so 更改为 hello_world_LDADD = -lgreetings

libgreetings.so 是一个仅用于开发的文件,因此它不会安装到映像中(除非您还安装了 libgreetings-dev 软件包 - 它就在那里)。

在交叉编译期间,您通常会像这样为 link 指定库: -lgreetings

因此将 hello_world_LDADD = $(libdir)/libgreetings.so 更改为 hello_world_LDADD = -lgreetings

我会从那个开始。通常,您不应该像在 makefile 中那样对它们进行硬编码,而是 'discover' 并检查 configure.ac 中的库(例如,使用 pkg-config,假设您的库安装了相应的 .pc 文件),然后设置适当的编译器和 linker 标志:

PKG_CHECK_MODULES(问候语,[问候语])

然后,在Makefile.am:

hello_world_LDADD = $(GREETINGS_LIBS)