How to include a simple C file in Yocto image by creating your own layer?

In this post, I will explain about creating your own layer in Yocto and then creating a simple C file which gets included in your final image(core-image-minimal).

So, first we will understand how to create your own layer in Yocto and then adding a simple C application into an final image.

Create your own layer
Basically there are two ways to create your own layer in Yocto:
  1. Manually 
  2. through Bitbake script
I will create a layer through Bitbake which is quite simpler and easier than doing it manually.

Assuming you have poky project downloaded from yocto's website, 
  • Navigate to poky project and set environment by running source oe-init-build-env
  • Now you got moved to "Build" directory, if you want to create a layer where other defaults layers(e.g. meta-poky etc.) are present then run "cd .."
  • Create a layer by running "bitbake-layers create-layer <meta-newlayer>" 
  • Navigate to build directory and run "bitbake-layers add-layer ../<meta-newlayer> which adds this layer into bblayer.conf file
  • That's it !! see below screenshot for reference,
Create a simple C app and recipe
Now we will create a simple C application and a recipe. A recipe is parsed to Bitbake and Bitbake further built this C app, installed onto a final image. Follow below steps:
  • Navigate to your own layer - cd meta-love
  • Create a recipe - mkdir recipes-love
  • Navigate to recipe dir and create a app dir - cd recipes-love & mkdir love
  • Navigate to app dir - cd love
  • Create another dir called files - mkdir files
  • Create a recipe with following content - vi love_0.1.bb

          SUMMARY = "Test Application"

          LICENSE = "CLOSED"

          SRC_URI = "file://love.c \
       "
          do_compile () {
          ${CC} ${CFLAGS} ${LDFLAGS} ${WORKDIR}/love.c -o ${WORKDIR}/love
          }

          do_install () {
         install -d ${D}${bindir}
         install -m 0755 ${WORKDIR}/love ${D}${bindir}/
          }
  • Navigate to files dir - cd files
  • Create a simple C file - vi love.c
          int main()
          {
              printf("Love is God\n");
              return 0;
          }
  • Add IMAGE_INSTALL_append = " love" in build/conf/local.conf file 
  • Navigate to build and run "bitbake core-image-minimal" (Make sure, you have set environment)
  • Go to "build/tmp/deploy/images/qemux86" and run "runqemu core-image-minimal"
  • Run your app




Comments

Popular posts from this blog

MBR partitioning with 'parted' utility

Replace default splash screen in Yocto

Disk Partitioning: MBR vs GPT