gpt4 book ai didi

c++ - 在 yocto 中编译 c++ 程序

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:39 27 4
gpt4 key购买 nike

我按照教程在我的 yocto 图像中包含了一个 C 程序。它工作得非常顺利,现在我的机器上运行了一个 helloworld 脚本。

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

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

最佳答案

假设你关注这个video关于创建 helloWorld.bbmeta-mylayer

这是在 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 来创建包。

关于c++ - 在 yocto 中编译 c++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43448747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com