Skip to content

Instantly share code, notes, and snippets.

@NotSoIntelligent
Created October 22, 2024 13:30
Show Gist options
  • Select an option

  • Save NotSoIntelligent/5974caaca40a100fb96d638c03b2d68f to your computer and use it in GitHub Desktop.

Select an option

Save NotSoIntelligent/5974caaca40a100fb96d638c03b2d68f to your computer and use it in GitHub Desktop.
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 a standard math library (libm) and the custom library.
DESCRIPTION = "Custom Library"
LICENSE = "MIT"
SRC_URI = "file://libcustom.c" # The source code for the custom library
# If your custom library has a Makefile, add the following to handle it.
inherit autotools
# This is the compilation step for the library
do_compile() {
# Compile the custom library
${CC} ${CFLAGS} -c libcustom.c -o libcustom.o
${AR} rcs libcustom.a libcustom.o
}
# This is the installation step for the library
do_install() {
# Install the static library (libcustom.a) to the library directory
install -d ${D}${libdir}
install -m 0755 libcustom.a ${D}${libdir}
# Install any public headers (if necessary)
install -d ${D}${includedir}
install -m 0644 libcustom.h ${D}${includedir}
}
DESCRIPTION = "My C Program using libm and libcustom"
LICENSE = "MIT"
SRC_URI = "file://myprogram.c" # Your C source file
# Add the custom library as a dependency
DEPENDS = "libcustom virtual/libc"
# This is the compilation step for your program
do_compile() {
# Compile and link the program with libm and libcustom
${CC} ${CFLAGS} myprogram.c -o myprogram -lm -lcustom
}
# This is the installation step for your program
do_install() {
# Install the program to the binary directory
install -d ${D}${bindir}
install -m 0755 myprogram ${D}${bindir}
}

Yocto Instructions for a Code with a dependant Standard and Custom Library

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.

1. Custom Library Recipe (libcustom)

The following is the Yocto recipe for building and installing the custom library (libcustom).

libcustom_1.0.bb:

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}
}

2. MyProgram Recipe (myprogram)

The following is the Yocto recipe for building the program (myprogram) that depends on a standard math library (libm) and the custom library (libcustom).

myprogram_1.0.bb:

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}
}

3. Explanation of bb files:

  • 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 the autotools class for configuring, building, and installing the package automatically.
  • DEPENDS: Lists the dependencies needed before myprogram can be compiled (in this case, libcustom and virtual/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.

4. Directory Structure Example

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

5. Layer Configuration

You need to add your custom layer (e.g., meta-yourlayer) to the Yocto build system. Follow these steps:

  1. 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
  2. Add the layer to your build configuration:

    • Open the conf/bblayers.conf file in your Yocto build directory.
    • Add your custom layer (meta-yourlayer) to BBLAYERS like this:
    BBLAYERS ?= "      /path/to/poky/meta      /path/to/poky/meta-poky      /path/to/meta-yourlayer    "
  3. Place your recipes in meta-yourlayer/recipes-custom/. Ensure that the myprogram_1.0.bb and libcustom_1.0.bb are in their respective directories as shown in the directory structure example.

6. Building the Recipes

To build both the custom library and your program, follow these steps:

  1. Build the custom library:
bitbake libcustom
  1. Build your program:
bitbake myprogram

7. Automated Build of Dependencies

Running 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment