在 yocto 中编译 c++ 程序

compile c++ program in yocto

我按照教程在我的 yocto 映像中包含了一个 C 程序。它就像一个魅力,现在我的机器上有一个 helloworld 脚本 运行。

我想对 C++ 做同样的事情,因为我需要加载一个使用 opencv 的程序。

我尝试将 c 更改为 cpp,但我还是失败了。我还需要更改什么才能使其正常工作? 你能给我指出任何教程或例子吗?我一直没能找到合适的,有一个简单的例子。

假设您在创建 helloWorld.bbmeta-mylayer

时遵循此 video

这是在 Yocto 中编译 C++ 文件的方式:

首先你需要了解Yocto中的GNU编译器和构建标志选项;在 bspdir/sources/poky/meta/conf/bitbake.conf 中有一个 bitbake 用于交叉编译的编译器列表:

TOOLCHAIN_OPTIONS = " --sysroot=${STAGING_DIR_TARGET}"

export CC = "${CCACHE}${HOST_PREFIX}gcc ${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS}"
export CXX = "${CCACHE}${HOST_PREFIX}g++ ${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS}"
export FC = "${CCACHE}${HOST_PREFIX}gfortran ${HOST_CC_ARCH}${TOOLCHAIN_OPTIONS}"
export CPP = "${HOST_PREFIX}gcc -E${TOOLCHAIN_OPTIONS} ${HOST_CC_ARCH}"
export LD = "${HOST_PREFIX}ld${TOOLCHAIN_OPTIONS} ${HOST_LD_ARCH}"
export CCLD = "${CC}"
export AR = "${HOST_PREFIX}ar"
export AS = "${HOST_PREFIX}as ${HOST_AS_ARCH}"
export RANLIB = "${HOST_PREFIX}ranlib"
export STRIP = "${HOST_PREFIX}strip"
export OBJCOPY = "${HOST_PREFIX}objcopy"
export OBJDUMP = "${HOST_PREFIX}objdump"
export STRINGS = "${HOST_PREFIX}strings"
export NM = "${HOST_PREFIX}nm"
PYTHON = "${@sys.executable}"

export BUILD_CC = "${CCACHE}${BUILD_PREFIX}gcc ${BUILD_CC_ARCH}"
export BUILD_CXX = "${CCACHE}${BUILD_PREFIX}g++ ${BUILD_CC_ARCH}"
export BUILD_FC = "${CCACHE}${BUILD_PREFIX}gfortran ${BUILD_CC_ARCH}"
export BUILD_CPP = "${BUILD_PREFIX}gcc ${BUILD_CC_ARCH} -E"
export BUILD_LD = "${BUILD_PREFIX}ld ${BUILD_LD_ARCH}"
export BUILD_CCLD = "${BUILD_PREFIX}gcc ${BUILD_CC_ARCH}"
export BUILD_AR = "${BUILD_PREFIX}ar"
export BUILD_AS = "${BUILD_PREFIX}as ${BUILD_AS_ARCH}"
export BUILD_RANLIB = "${BUILD_PREFIX}ranlib"
export BUILD_STRIP = "${BUILD_PREFIX}strip"
export BUILD_NM = "${BUILD_PREFIX}nm"

export MAKE = "make"
EXTRA_OEMAKE = "-e MAKEFLAGS="
EXTRA_OECONF = ""
export LC_ALL = "C"

执行bitbake -e | grep CXX,你会看到工具链直接指向了哪里。

export CXX="arm-poky-linux-gnueabi-g++  -march=armv7-a -marm  -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 --sysroot=/mountdata/charles/hio-yocto-bsp/jethro/build/tmp/sysroots/hio-imx6dl-board"

现在,了解了工具链背景,我们将使用CXX来编译helloworld recipe;

meta-mylayer/recipes-example/example/helloworld-0.1/helloworld.c更改为helloworld.cpp

#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
    cout << "hello World "<< endl;
    return 0;
}

并修改helloworld.bb。请注意,我已将 {CC} 更改为 {CXX},并且 helloworld.c 更改为 helloworld.cpp

SUMMARY = "Simple helloworld application"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://helloworld.cpp"

S = "${WORKDIR}"

do_compile() {
         ${CXX} helloworld.cpp -o helloworld
}

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

现在您可以使用 bitbake helloworld 创建程序包了。