在yocto中编译c ++程序

时间:2017-04-17 09:29:22

标签: c++ yocto bitbake

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

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

我尝试用cpp改变c但是因为我失败了。还有什么需要改变以使其工作? 你能指点我那里的任何教程或例子吗?我还没有找到一个合适的例子。

1 个答案:

答案 0 :(得分:7)

假设您在创建helloWorld.bbmeta-mylayer

时遵循此https://jsfiddle.net/marco321/beah5v0d/

这是你在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配方;

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创建包。