Files
u-boot/examples/ulib
Simon Glass 4c32ae1087 ulib: Expand the ulib example to have two files
It is possible to have some files that build in the system environment
and some in the U-Boot environment. To build for the system, the system
headers must be used, or at least have priority. For a U-Boot file, its
headers must be used exclusively.

Expand the Makefile example to have two files, one of which is built
using U-Boot headers. This shows how a program can be built which
straddles both domains.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-09-08 13:10:53 -06:00
..

U-Boot Library Example
======================

This directory contains example programs showing how to use the U-Boot library
(libu-boot.so) in external C programs.

Building U-Boot Library
-----------------------

First, build U-Boot with library support:

    make O=/tmp/b/sandbox -j$(nproc) sandbox_defconfig all

This creates:
- /tmp/b/sandbox/libu-boot.so (shared library)
- /tmp/b/sandbox/libu-boot.a (static library)

Example Programs
----------------

The examples are built automatically as part of the U-Boot build. So far there
is only one.

**demo.c** - Demonstrates using U-Boot library functions

- Shows how to init the library with ulib_init()
- Uses U-Boot's OS functions (os_open(), os_fgets(), os_close())
- Reads and displays system information
- Shows the U-Boot version

Building Examples
-----------------

The examples are built automatically when U-Boot has these options enabled::

    CONFIG_ULIB=y
    CONFIG_EXAMPLES=y

To build manually:

    # From this directory examples/ulib
    make UBOOT_BUILD=/tmp/b/sandbox/ srctree=../..

Running Examples
----------------

    # Run the demo
    LD_LIBRARY_PATH=/tmp/b/sandbox ./demo

    # Run the demo (static version)
    ./demo_static

Key Points
----------

- Avoid including U-Boot headers that conflict with system headers. This
  Makefile gives priority to the system headers
- Use ulib_init() to init the library
- Use ulib_uninit() to clean up
- Set LD_LIBRARY_PATH when running dynamically linked programs

Copying for External Use
-------------------------

This directory can be copied elsewhere and used independently:

    cp -r examples/ulib ~/my-project/
    cd ~/my-project/ulib
    make UBOOT_BUILD=/path/to/u-boot-build  srctree=/path/to/u-boot-source

License
-------

All examples are licensed under GPL-2.0+