Search code examples
c++yoctobitbake

compile c++ program in yocto


I have followed a tutorial to include a C program in my yocto image. It worked like a charm and now I have a helloworld script running on my machine.

I want to do the same with c++ because I need to load a program which makes use of opencv.

I tried by changing c with cpp but as I though, it failed. what else do I need to change to make this working? Can you point me to any tutorial or example out there? I haven't been able to find a suitable one with a simple example.


Solution

  • Assume that you follow this video on creating helloWorld.bb and meta-mylayer

    This is how you would compile a C++ file in Yocto:

    First you need to understand what the GNU compiler and build flag option in Yocto; In bspdir/sources/poky/meta/conf/bitbake.conf There is a list of compilers which bitbake uses to cross compile:

    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"
    

    Execute bitbake -e | grep CXX , you will see where the toolchain is directly pointed to.

    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"
    

    Now, understood where the toolchain background, we will use CXX to compile helloworld recipe;

    Change meta-mylayer/recipes-example/example/helloworld-0.1/helloworld.c to helloworld.cpp

    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "hello World "<< endl;
        return 0;
    }
    

    and modify helloworld.bb. take note that I have changed from {CC} to {CXX} and the helloworld.c changed to 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}
    }
    

    Now you can use bitbake helloworld to create the package.