How to compile a basic c file in yocto

风格不统一 提交于 2019-12-05 08:14:03

问题


I am working on yocto, I want to compile some C files in yocto and install the resulting binary to external filesystem. Before doing that I tried creating a separate reciepe and compile c code from it. I am unable to compile it.


回答1:


I am not sure to understand the question since it is not precise enough.

Including C files in recipe tree

If you want to have the C files in your recipe, having a file tree like this:

recipe-example/example/example_0.1.bb
recipe-example/example/example-0.1/helloworld.c

You can generate this example when you create a new layer using

yocto-layer <your-layer-name>

Your bb file will look like this:

#
# This file was derived from the 'Hello World!' example recipe in the
# Yocto Project Development Manual.
#
SUMMARY = "Simple helloworld application"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "file://helloworld.c"

S = "${WORKDIR}"

do_compile() {
         ${CC} helloworld.c -o helloworld
}

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

It will compile the hello world file and install it into /usr/bin on your image.

From a Git repo

You also can compile from a git repository, I advise you to read the manual and examples in your yocto folder. Here is an example here of wiringPi:

DESCRIPTION = "A library to control Raspberry Pi GPIO channels"
HOMEPAGE = "https://projects.drogon.net/raspberry-pi/wiringpi/"
SECTION = "devel/libs"
LICENSE = "LGPLv3+"
LIC_FILES_CHKSUM = "file://COPYING.LESSER;md5=e6a600fd5e1d9cbde2d983680233ad02"

# tag 2.29
SRCREV = "d79506694d7ba1c3da865d095238289d6175057d"

S = "${WORKDIR}/git"

SRC_URI = "git://git.drogon.net/wiringPi \
           file://0001-Add-initial-cross-compile-support.patch \
           file://0001-include-asm-ioctl.h-directly-for-_IOC_SIZEBITS.patch \
           "

COMPATIBLE_MACHINE = "raspberrypi"

CFLAGS_prepend = "-I${S}/wiringPi -I${S}/devLib"

EXTRA_OEMAKE += "'INCLUDE_DIR=${D}${includedir}' 'LIB_DIR=${D}${libdir}'"
EXTRA_OEMAKE += "'DESTDIR=${D}/usr' 'PREFIX=""'"

do_compile() {
    oe_runmake -C devLib
    oe_runmake -C wiringPi
    oe_runmake -C gpio 'LDFLAGS=${LDFLAGS} -L${S}/wiringPi -L${S}/devLib'
}

do_install() {
    oe_runmake -C devLib install
    oe_runmake -C wiringPi install
    oe_runmake -C gpio install
}

It is fetching from a git repository, applying patches generated by git, using oe_runmake to compile with the makefiles.

With devtool

It has been asked in a comment on how to add a recipe with devtool. We will still use wiringPi as an example again. Download it doing https://github.com/WiringPi/WiringPi The Makefile is is the folder wiringPi. You can then do

devtool add <name_of_recipe> <path_to_Makefile_folder>

Take care of the warning from devtool

NOTE: Creating workspace layer in /home/dbensoussan/new_poky/poky/build/workspace
NOTE: Enabling workspace layer in bblayers.conf
NOTE: Using source tree as build directory since that would be the default for this recipe
NOTE: Recipe /home/dbensoussan/new_poky/poky/build/workspace/recipes/project/project.bb has been automatically created; further editing may be required to make it fully functional

This is generating the recipe as follow:

# Recipe created by recipetool
# This is the basis of a recipe and may need further editing in order to be fully functional.
# (Feel free to remove these comments when editing.)
#
# WARNING: the following LICENSE and LIC_FILES_CHKSUM values are best guesses - it is
# your responsibility to verify that the values are complete and correct.
LICENSE = "Unknown"
LIC_FILES_CHKSUM = "file://COPYING.LESSER;md5=e6a600fd5e1d9cbde2d983680233ad02"

# No information for SRC_URI yet (only an external source tree was specified)
SRC_URI = ""


# NOTE: this is a Makefile-only piece of software, so we cannot generate much of the
# recipe automatically - you will need to examine the Makefile yourself and ensure
# that the appropriate arguments are passed in.

do_configure () {
    # Specify any needed configure commands here
    :
}

do_compile () {
    # You will almost certainly need to add additional arguments here
    oe_runmake
}

do_install () {
    # This is a guess; additional arguments may be required
    oe_runmake install 'DESTDIR=${D}'
}

You can then edit your recipe to suit your configuration




回答2:


You can just go to the official documentation to find your answer.

The very first example is exactly what you need.



来源:https://stackoverflow.com/questions/37705995/how-to-compile-a-basic-c-file-in-yocto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!