C Standard Library
Zephyr provides several C standard libraries for users:
-
Generic C Library -
Minimal libc -
Newlib -
Picolibc
Generic C Library
The Generic C Library is not a complete C standard library; it provides some C library functions to be used in conjunction with other C standard libraries, mainly to address:
-
Providing functionalities that other C standard libraries do not have -
Replacing functionalities in other C libraries with code that is more suitable for use in the Zephyr environment. The implementation can be found in zephyr/lib/libc/common
, including:
-
time
-
strnlen
-
abort
-
Dynamic memory management, malloc
related functions, based on sys_heap for memory management.
For sys_heap, refer to Zephyr Memory Management – Heap[1]
Minimal libc
Before version 3.5.0, Minimal libc was the default standard C library for Zephyr. In Zephyr, Minimal libc implements the minimal subset of ISO/IEC 9899:2011 standard C library functions, meeting the requirements defined by Zephyr Coding Guidelines Rule A.4[2] for the Zephyr kernel. This library wraps the cbprintf()
function for formatted output. Refer to Zephyr Formatted Output – cbprintf[3]
This library uses the dynamic memory management of the Generic C Library.
This library defines its own erron.h but strives to remain consistent with POSIX.
Newlib
Newlib is a complete C library implementation designed for embedded systems. It is a separate open-source project, not appearing in source code form in Zephyr, but compiled into libc.a and libm.a packaged within the Zephyr SDK, with each architecture in the SDK having its own libc.a and libm.a.
In Zephyr, the file zephyr/lib/libc/newlib/libc-hooks.c
implements the hook functions required by the Newlib standard library functions, providing the following hook implementations:
-
Heap Management -
open/close/read/write …. -
lock -
time -
stdin/stdout -
errno
Newlib provides two libraries:
-
CONFIG_NEWLIB_LIBC: Full Newlib Library -
CONFIG_NEWLIB_LIBC_NANO: Nano Newlib Library libc_nano.a, libm_nano.a
The Newlib nano variant (libc_nano.a and libm_nano.a) is a size-optimized version of Newlib that supports all functionalities of the full variant.
Note: The Newlib nano variant is not suitable for all architectures. The availability of the nano variant is specified by CONFIG_HAS_NEWLIB_LIBC_NANO, and you can search whether your SOC architecture has nano Newlib through this configuration option.
Newlib uses its own dynamic memory management algorithm, managing memory dynamically through the heap management hook functions.
Picolibc
Picolibc is a complete C library implementation designed for embedded systems, targeting C17 (ISO/IEC 9899:2018) and POSIX 2018 (IEEE Std 1003.1-2017) standards. Picolibc is an external open-source project that can be provided as a module to Zephyr, and the Zephyr SDK also precompiles the Picolib library for each supported architecture.
Starting from v3.5.0, Picolibc is used as the default standard C library for Zephyr.
In Zephyr, the file zephyr/lib/libc/picolib/libc-hooks.c
implements the hook functions required by the Newlib standard library functions, providing the following hook implementations:
-
lock -
stdin/stdout -
errno
When configured to use Picolib, dynamic memory management uses the implementation under the Generic C Library.
Third-Party Toolchains
Zephyr supports third-party toolchains, some of which have their own standard C libraries that require Zephyr to implement hook functions:
-
Arm Compiler 6: Hook functions implemented in zephyr/lib/libc/armstdc -
DesignWare ARC MetaWare Development Toolkit (MWDT): Hook functions implemented in zephyr/lib/libc/arcmwdt
References
https://docs.zephyrproject.org/3.5.0/develop/languages/c/index.html#standard-library
Reference Material
Zephyr Memory Management – Heap
[2]Coding Guidelines Rule A.4: https://docs.zephyrproject.org/3.4.0/contribute/coding_guidelines/index.html#coding-guideline-libc-usage-restrictions-in-zephyr-kernel
[3]Zephyr Formatted Output – cbprintf