Part 1
요약·해설
C99 기능과 사실상 필요한 C11, 네 C library의 문서 계층, formatted output 설정 차이, userspace heap alignment·partition 규칙과 native kernel API 권고를 다룹니다.
C99 · C11 · _Generic
Minimal · Newlib · Picolibc
malloc · Alignment · Userspace
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
386867C3D92672AD508B965E2FDEF89E39025C1262387ED4146D30636CCFD305
.. _language_c:
C Language Support
##################
C is a general-purpose low-level programming language that is widely used for
writing code for embedded systems.
Zephyr is primarily written in C and natively supports applications written in
the C language. All Zephyr API functions and macros are implemented in C and
available as part of the C header files under the :file:`include` directory, so
writing Zephyr applications in C gives the developers access to the most
features.
The ``main()`` function must have the return type of ``int`` as Zephyr
applications run in a "hosted" environment as defined by the C
standard. Applications must return zero (0) from main. All non-zero return
values are reserved.
.. _c_standards:
Language Standards
******************
Zephyr does not target a specific version of the C standards; however, the
Zephyr codebase makes extensive use of the features newly introduced in the
1999 release of the ISO C standard (ISO/IEC 9899:1999, hereinafter referred to
as C99) such as those listed below, effectively requiring the use of a compiler
toolchain that supports the C99 standard and above:
* inline functions
* standard boolean types (``bool`` in ``<stdbool.h>``)
* fixed-width integer types (``[u]intN_t`` in ``<stdint.h>``)
* designated initializers
* variadic macros
* ``restrict`` qualification
Some Zephyr components make use of the features newly introduced in the 2011
release of the ISO C standard (ISO/IEC 9899:2011, hereinafter referred to as
C11) such as the type-generic expressions using the ``_Generic`` keyword. For
example, the :c:func:`cbprintf` component, used as the default formatted output
processor for Zephyr, makes use of the C11 type-generic expressions, and this
effectively requires most Zephyr applications to be compiled using a compiler
toolchain that supports the C11 standard and above.
In summary, it is recommended to use a compiler toolchain that supports at
least the C11 standard for developing with Zephyr. It is, however, important to
note that some optional Zephyr components and external modules may make use of
the C language features that have been introduced in more recent versions of
the standards, in which case it will be necessary to use a more up-to-date
compiler toolchain that supports such standards.
.. _c_library:
Standard Library
****************
The `C Standard Library`_ is an integral part of any C program, and Zephyr
provides the support for a number of different C libraries for the applications
to choose from, depending on the compiler toolchain being used to build the
application.
.. toctree::
:maxdepth: 2
common_libc.rst
minimal_libc.rst
newlib.rst
picolibc.rst
.. _`C Standard Library`: https://en.wikipedia.org/wiki/C_standard_library
.. _c_library_formatted_output:
Formatted Output
****************
C defines standard formatted output functions such as ``printf`` and
``sprintf`` and these functions are implemented by the C standard
libraries.
Each C standard library has its own set of requirements and configurations for
selecting the formatted output modes and capabilities. Refer to each C standard
library documentation for more details.
.. _c_library_dynamic_mem:
Dynamic Memory Management
*************************
C defines a standard dynamic memory management interface (for example,
:c:func:`malloc` and :c:func:`free`) and these functions are implemented by the
C standard libraries.
While the details of the dynamic memory management implementation varies across
different C standard libraries, all supported libraries must conform to the
following conventions. Every supported C standard library shall:
* manage its own memory heap either internally or by invoking the hook
functions (for example, :c:func:`sbrk`) implemented in :file:`libc-hooks.c`.
* maintain the architecture- and memory region-specific alignment requirements
for the memory blocks allocated by the standard dynamic memory allocation
interface (for example, :c:func:`malloc`).
* allocate memory blocks inside the ``z_malloc_partition`` memory partition
when userspace is enabled. See :ref:`memory_domain_predefined_partitions`.
For more details regarding the C standard library-specific memory management
implementation, refer to each C standard library documentation.
.. note::
Native Zephyr applications should use the :ref:`memory management API
<memory_management_api>` supported by the Zephyr kernel such as
:c:func:`k_malloc` in order to take advantage of the advanced features
that they offer.
C standard dynamic memory management interface functions such as
:c:func:`malloc` should be used only by the portable applications and
libraries that target multiple operating systems.
Part 3
한국어 전문 번역
C 언어 지원
C는 embedded system code 작성에 널리 쓰이는 범용 low-level programming language입니다.
Zephyr는 주로 C로 작성되었고 C application을 native로 지원합니다. 모든 Zephyr API function과 macro는 C로 구현되어 include directory 아래의 C header file로 제공되므로, C로 Zephyr application을 작성하면 가장 많은 기능에 접근할 수 있습니다.
main() function의 return type은 int여야 합니다. Zephyr application은 C standard가 정의하는 hosted 환경에서 실행되기 때문입니다. Application은 main에서 0을 return해야 하며 0이 아닌 모든 return value는 예약되어 있습니다.
언어 standard
Zephyr는 특정 C standard version 하나를 target으로 삼지 않습니다. 하지만 codebase가 1999년 ISO C standard(ISO/IEC 9899:1999, C99)에 새로 들어온 다음 기능을 폭넓게 사용하므로 사실상 C99 이상을 지원하는 compiler toolchain이 필요합니다.
- Inline function
- 표준 boolean type(
<stdbool.h>의bool) - 고정 폭 integer type(
<stdint.h>의[u]intN_t) - Designated initializer
- Variadic macro
restrictqualifier
일부 Zephyr component는 2011년 ISO C standard(ISO/IEC 9899:2011, C11)에 추가된 기능도 사용합니다. 예를 들어 Zephyr의 기본 formatted output processor인 cbprintf() component는 _Generic keyword의 type-generic expression을 사용합니다. 따라서 대부분의 Zephyr application은 실질적으로 C11 이상을 지원하는 compiler toolchain으로 compile해야 합니다.
요약하면 Zephyr 개발에는 최소 C11을 지원하는 compiler toolchain을 권장합니다. 다만 optional Zephyr component나 외부 module이 더 최신 standard에 추가된 C 기능을 사용할 수 있으므로, 그런 component를 사용한다면 해당 standard를 지원하는 더 최신 toolchain이 필요합니다.
표준 library
C Standard Library는 모든 C program의 핵심 구성 요소입니다. Zephyr는 application build에 사용하는 compiler toolchain에 따라 선택할 수 있도록 여러 C library를 지원합니다.
Formatted output
C는 printf와 sprintf 같은 표준 formatted output function을 정의하며 C standard library가 이를 구현합니다.
Formatted output mode와 capability를 선택하는 requirement·configuration은 C standard library마다 다릅니다. 자세한 내용은 각 C standard library 문서를 참고하십시오.
동적 memory 관리
C는 malloc()과 free() 같은 표준 동적 memory 관리 interface를 정의하며 C standard library가 이 function을 구현합니다.
구현 세부 사항은 C standard library마다 다르지만 지원되는 모든 library는 다음 규칙을 따라야 합니다.
- Memory heap을 내부에서 직접 관리하거나
libc-hooks.c에 구현된sbrk()같은 hook function을 호출해 관리합니다. malloc()같은 표준 동적 memory allocation interface가 할당한 memory block에 대해 architecture와 memory region별 alignment requirement를 유지합니다.- Userspace가 활성화되면
z_malloc_partitionmemory partition 안에서 memory block을 할당합니다. 미리 정의된 memory partition을 참고하십시오.
각 C standard library의 구체적인 memory 관리 구현은 해당 library 문서를 참고하십시오.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.