This gist provides step-by-step instructions on how to set up and build a Yocto project that includes a custom library (libcustom) and a program (myprogram) that depends on both the standard math library (libm) and the custom library.
The following is the Yocto recipe for building and installing the custom library (libcustom).
DESCRIPTION = "Custom Library"
LICENSE = "MIT"
SRC_URI = "file://libcustom.c" # The source code for the custom library
inherit autotools
do_compile() {
${CC} ${CFLAGS} -c libcustom.c -o libcustom.o
${AR} rcs libcustom.a libcustom.o
}
do_install() {
install -d ${D}${libdir}
install -m 0755 libcustom.a ${D}${libdir}
install -d ${D}${includedir}
install -m 0644 libcustom.h ${D}${includedir}
}The following is the Yocto recipe for building the program (myprogram) that depends on a standard math library (libm) and the custom library (libcustom).
DESCRIPTION = "My C Program using libm and libcustom"
LICENSE = "MIT"
SRC_URI = "file://myprogram.c" # Your C source file
DEPENDS = "libcustom virtual/libc"
do_compile() {
${CC} ${CFLAGS} myprogram.c -o myprogram -lm -lcustom
}
do_install() {
install -d ${D}${bindir}
install -m 0755 myprogram ${D}${bindir}
}DESCRIPTION: A short description of what the custom library does.LICENSE: The license under which the code is distributed (in this case, MIT).SRC_URI: The location of the source file (libcustom.c). This file will be compiled into the library.inherit autotools: Inherits theautotoolsclass for configuring, building, and installing the package automatically.DEPENDS: Lists the dependencies needed beforemyprogramcan be compiled (in this case,libcustomandvirtual/libc).do_compile(): The manual compilation process, using${CC}and${AR}to compile the source and create a static library.do_install(): The install step, where the built files (library and headers) are copied into the appropriate directories in the Yocto build environment.
Ensure that both recipes are in the correct layer. If you don't have a custom layer, you can create one. Here is an example of what your directory structure should look like:
meta-yourlayer/
├── recipes-custom/
│ ├── libcustom/
│ │ ├── libcustom_1.0.bb
│ │ └── files/
│ │ ├── libcustom.c
│ │ └── libcustom.h
│ ├── myprogram/
│ │ ├── myprogram_1.0.bb
│ │ └── files/
│ │ └── myprogram.c
You need to add your custom layer (e.g., meta-yourlayer) to the Yocto build system. Follow these steps:
-
Create a custom layer: If you don't have a custom layer, you can create one using
bitbake-layers:bitbake-layers create-layer meta-yourlayer
-
Add the layer to your build configuration:
- Open the
conf/bblayers.conffile in your Yocto build directory. - Add your custom layer (
meta-yourlayer) toBBLAYERSlike this:
BBLAYERS ?= " /path/to/poky/meta /path/to/poky/meta-poky /path/to/meta-yourlayer "
- Open the
-
Place your recipes in
meta-yourlayer/recipes-custom/. Ensure that themyprogram_1.0.bbandlibcustom_1.0.bbare in their respective directories as shown in the directory structure example.
To build both the custom library and your program, follow these steps:
- Build the custom library:
bitbake libcustom- Build your program:
bitbake myprogramRunning the command bitbake myprogram will automatically build the custom library (libcustom) because it's declared as a dependency in the myprogram_1.0.bb recipe. Yocto checks the DEPENDS field and ensures that all dependencies are built before compiling myprogram.