blob: 0e4fe9a3ae1a70935e856efb5c68c3eaa058d70c [file] [log] [blame]
<!--
Documentation/_templates/layout.html
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. The
ASF licenses this file to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
-->
<!DOCTYPE html>
<html class="writer-html5" lang="en">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.19: https://docutils.sourceforge.io/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Integrating with Newlib &mdash; NuttX latest documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/design-style.1e8bd061cd6da7fc9cf755528e8ffc24.min.css" />
<link rel="stylesheet" type="text/css" href="../_static/custom.css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<script src="../_static/jquery.js"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<script src="../_static/design-tabs.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="NuttX Protected Build" href="protected_build.html" />
<link rel="prev" title="IPv6" href="ipv6.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> NuttX
</a>
<!-- this version selector is quite ugly, should be probably replaced by something
more modern -->
<div class="version-selector">
<select onchange="javascript:location.href = this.value;">
<option value="../../latest" selected="selected">latest</option>
<option value="../../10.0.0" >10.0.0</option>
<option value="../../10.0.1" >10.0.1</option>
<option value="../../10.1.0" >10.1.0</option>
<option value="../../10.2.0" >10.2.0</option>
<option value="../../10.3.0" >10.3.0</option>
<option value="../../11.0.0" >11.0.0</option>
<option value="../../12.0.0" >12.0.0</option>
<option value="../../12.1.0" >12.1.0</option>
<option value="../../12.2.0" >12.2.0</option>
<option value="../../12.2.1" >12.2.1</option>
<option value="../../12.3.0" >12.3.0</option>
<option value="../../12.4.0" >12.4.0</option>
<option value="../../12.5.0" >12.5.0</option>
<option value="../../12.5.1" >12.5.1</option>
<option value="../../12.6.0" >12.6.0</option>
<option value="../../12.7.0" >12.7.0</option>
<option value="../../12.8.0" >12.8.0</option>
<option value="../../12.9.0" >12.9.0</option>
<option value="../../12.10.0" >12.10.0</option>
<option value="../../12.11.0" >12.11.0</option>
</select>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Table of Contents</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Home</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/index.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../introduction/inviolables.html">The Inviolable Principles of NuttX</a></li>
<li class="toctree-l1"><a class="reference internal" href="../platforms/index.html">Supported Platforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../components/index.html">OS Components</a></li>
<li class="toctree-l1"><a class="reference internal" href="../applications/index.html">Applications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../implementation/index.html">Implementation Details</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../debugging/index.html">Debugging</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Guides</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="nfs.html">NFS Client How-To</a></li>
<li class="toctree-l2"><a class="reference internal" href="usbtrace.html">USB Device Trace</a></li>
<li class="toctree-l2"><a class="reference internal" href="simulator.html">Simulator</a></li>
<li class="toctree-l2"><a class="reference internal" href="rndis.html">How to use RNDIS</a></li>
<li class="toctree-l2"><a class="reference internal" href="drivers.html">Drivers</a></li>
<li class="toctree-l2"><a class="reference internal" href="cpp_cmake.html">C++ Example using CMake</a></li>
<li class="toctree-l2"><a class="reference internal" href="pysimcoder.html">pysimCoder integration with NuttX</a></li>
<li class="toctree-l2"><a class="reference internal" href="customboards.html">Custom Boards How-To</a></li>
<li class="toctree-l2"><a class="reference internal" href="customapps.html">Custom Apps How-to</a></li>
<li class="toctree-l2"><a class="reference internal" href="citests.html">Running CI Test Locally</a></li>
<li class="toctree-l2"><a class="reference internal" href="zerolatencyinterrupts.html">High Performance: Zero Latency Interrupts, Maskable Nested Interrupts</a></li>
<li class="toctree-l2"><a class="reference internal" href="fortify.html">Fortify</a></li>
<li class="toctree-l2"><a class="reference internal" href="nestedinterrupts.html">Nested Interrupts</a></li>
<li class="toctree-l2"><a class="reference internal" href="ofloader.html">Open Flash Loader</a></li>
<li class="toctree-l2"><a class="reference internal" href="testingtcpip.html">Testing TCP/IP Network Stacks</a></li>
<li class="toctree-l2"><a class="reference internal" href="automounter.html">Auto-Mounter</a></li>
<li class="toctree-l2"><a class="reference internal" href="stm32nullpointer.html">STM32 Null Pointer Detection</a></li>
<li class="toctree-l2"><a class="reference internal" href="stm32ccm.html">STM32 CCM Allocator</a></li>
<li class="toctree-l2"><a class="reference internal" href="etcromfs.html">etc romfs</a></li>
<li class="toctree-l2"><a class="reference internal" href="thread_local_storage.html">Thread Local Storage</a></li>
<li class="toctree-l2"><a class="reference internal" href="devicetree.html">Device Tree</a></li>
<li class="toctree-l2"><a class="reference internal" href="changing_systemclockconfig.html">Changing the System Clock Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="usingkernelthreads.html">Using Kernel Threads</a></li>
<li class="toctree-l2"><a class="reference internal" href="armv7m_runtimestackcheck.html">ARMv7-M Run Time Stack Checking</a></li>
<li class="toctree-l2"><a class="reference internal" href="include_files_board_h.html">Including Files in board.h</a></li>
<li class="toctree-l2"><a class="reference internal" href="specialstuff_in_nuttxheaderfiles.html">Why can’t I put my special stuff in NuttX header files?</a></li>
<li class="toctree-l2"><a class="reference internal" href="kernel_threads_with_custom_stacks.html">Kernel Threads with Custom Stacks</a></li>
<li class="toctree-l2"><a class="reference internal" href="versioning_and_task_names.html">Versioning and Task Names</a></li>
<li class="toctree-l2"><a class="reference internal" href="logging_rambuffer.html">Logging to a RAM Buffer</a></li>
<li class="toctree-l2"><a class="reference internal" href="ipv6.html">IPv6</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Integrating with Newlib</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#built-in-c-library">Built-In C Library</a></li>
<li class="toctree-l3"><a class="reference internal" href="#header-file-issues">Header File Issues</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#math-h">math.h</a></li>
<li class="toctree-l4"><a class="reference internal" href="#cmath">cmath</a></li>
<li class="toctree-l4"><a class="reference internal" href="#alloca-h">alloca.h</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#c-issues">C++ Issues</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#new-operator">new Operator</a></li>
<li class="toctree-l4"><a class="reference internal" href="#uint32-t">uint32_t</a></li>
<li class="toctree-l4"><a class="reference internal" href="#size-t">size_t</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="protected_build.html">NuttX Protected Build</a></li>
<li class="toctree-l2"><a class="reference internal" href="platform_directories.html">Platform Directories</a></li>
<li class="toctree-l2"><a class="reference internal" href="port_drivers_to_stm32f7.html">Porting Drivers to the STM32 F7</a></li>
<li class="toctree-l2"><a class="reference internal" href="semihosting.html">Semihosting</a></li>
<li class="toctree-l2"><a class="reference internal" href="renode.html">Run NuttX on Renode</a></li>
<li class="toctree-l2"><a class="reference internal" href="signal_events_interrupt_handlers.html">Signaling Events from Interrupt Handlers</a></li>
<li class="toctree-l2"><a class="reference internal" href="signaling_sem_priority_inheritance.html">Signaling Semaphores and Priority Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="smaller_vector_tables.html">Smaller Vector Tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="port.html">How to port</a></li>
<li class="toctree-l2"><a class="reference internal" href="updating_release_system_elf.html">Updating a Release System with ELF Programs</a></li>
<li class="toctree-l2"><a class="reference internal" href="partially_linked_elf.html">ELF Programs – With Symbol Tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="fully_linked_elf.html">ELF Programs – No Symbol Tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="building_nuttx_with_app_out_of_src_tree.html">Building NuttX with Applications Outside the Source Tree</a></li>
<li class="toctree-l2"><a class="reference internal" href="building_uclibcpp.html">Building uClibc++</a></li>
<li class="toctree-l2"><a class="reference internal" href="custom_app_directories.html">Custom Application Directories</a></li>
<li class="toctree-l2"><a class="reference internal" href="multiple_nsh_sessions.html">Multiple NSH Sessions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nsh_network_link_management.html">NSH Network Link Management</a></li>
<li class="toctree-l2"><a class="reference internal" href="ram_rom_disks.html">RAM Disks and ROM Disks</a></li>
<li class="toctree-l2"><a class="reference internal" href="reading_can_msgs.html">Reading CAN Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="remove_device_drivers_nsh.html">Removing Device Drivers with NSH</a></li>
<li class="toctree-l2"><a class="reference internal" href="rust.html">Rust in NuttX</a></li>
<li class="toctree-l2"><a class="reference internal" href="optee.html">Interfacing with OP-TEE</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../logos/index.html">NuttX Logos</a></li>
<li class="toctree-l1"><a class="reference internal" href="../_tags/tagsindex.html">Tags</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">NuttX</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="index.html">Guides</a></li>
<li class="breadcrumb-item active">Integrating with Newlib</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/apache/nuttx/blob/master/Documentation/guides/integrate_newlib.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="integrating-with-newlib">
<h1>Integrating with Newlib<a class="headerlink" href="#integrating-with-newlib" title="Permalink to this heading"></a></h1>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Migrated from:
<a class="reference external" href="https://cwiki.apache.org/confluence/display/NUTTX/Integrating+with+Newlib">https://cwiki.apache.org/confluence/display/NUTTX/Integrating+with+Newlib</a></p>
</div>
<section id="built-in-c-library">
<h2>Built-In C Library<a class="headerlink" href="#built-in-c-library" title="Permalink to this heading"></a></h2>
<p>NuttX has its own, tiny, built-in C library. Along with that C library are
all of the headers files with the definitions specific to that built-in C
library. The definitions in those header files are not compatible with the
definitions that you will find in the header files that come with any other
C library and trying to mix these with header files from other C libraries
is bound to cause you problems.</p>
<p>When GCC is built, it is built against a C library. The NuttX <cite>buildroot</cite>
tools are, of course, built against the built-in NuttX C-library and
would seem to be the obvious choice of tools to use. But there are
many reasons to use other tool chains. As examples, the NuttX <cite>buildroot</cite>
tools have some limitations in C++ support. Another example, is that you
might want to use the higher-performance math library that is included
in some other implementation of the C library.</p>
<p>There are many C libraries available: <cite>glibc</cite> and <cite>uClibc</cite> are commonly
used with Linux tools. These should be avoided. Most embedded toolchains
will be built against <cite>newlib</cite>. So if you are not using the NuttX buildroot
toolchain, you will most likely be using a toolchain that has <cite>newlib</cite> built
into it. Because of this, you may see issues if you include <cite>newlib</cite> header
files into your NuttX code.</p>
</section>
<section id="header-file-issues">
<h2>Header File Issues<a class="headerlink" href="#header-file-issues" title="Permalink to this heading"></a></h2>
<section id="math-h">
<h3>math.h<a class="headerlink" href="#math-h" title="Permalink to this heading"></a></h3>
<p>Nuttx includes a built-in math library that can be selected with
<code class="docutils literal notranslate"><span class="pre">CONFIG_LIBM=y</span></code>. There are reasons to use an external math library,
however: The NuttX math library is written in C and will not be as
performant as a custom math library tuned for your processor
architecture. There some addition issues with the NuttX math
libraries as documented in the top-level TODO list.</p>
<p>Many people choose to use the <cite>newlib</cite> math library. If you include
<code class="docutils literal notranslate"><span class="pre">math.h</span></code> without selecting <code class="docutils literal notranslate"><span class="pre">CONFIG_LIBM=y</span></code>, you will probably get the
<cite>newlib</cite> math library and you will certainly see a compilation error
involving the definition of the type <code class="docutils literal notranslate"><span class="pre">wint_t</span></code>.</p>
<p>There have been many work arounds described in the NuttX forum.
Here are a few:</p>
<ul class="simple">
<li><p>Copy the newlib <code class="docutils literal notranslate"><span class="pre">math.h</span></code> to <code class="docutils literal notranslate"><span class="pre">nuttx/include/math.h</span></code> and remove
the reference to <code class="docutils literal notranslate"><span class="pre">wint_t</span></code>.</p></li>
<li><p>Add the following to <code class="docutils literal notranslate"><span class="pre">nuttx/libc/stdio/lib_libvsprintf.c</span></code>. I
especially dislike this solution because it involves modification
to a NuttX header file that cannot be accepted upstream.</p></li>
</ul>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* Include floating point functions */</span>
<span class="cp">#ifdef CONFIG_LIBC_FLOATINGPOINT</span>
<span class="cp">#</span><span class="w"> </span><span class="cp">include</span><span class="w"> </span><span class="cpf">&quot;wchar.h&quot;</span>
<span class="cp">#</span><span class="w"> </span><span class="cp">include</span><span class="w"> </span><span class="cpf">&quot;stdio/lib_libdtoa.c&quot;</span>
<span class="cp">#endif</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Provide your own version of <code class="docutils literal notranslate"><span class="pre">math.h</span></code> (for GCC only) containing the
following. And add the path to this <code class="docutils literal notranslate"><span class="pre">math.h</span></code> to your <code class="docutils literal notranslate"><span class="pre">CFLAGS</span></code> include
path arguments. The path can specified by adding <code class="docutils literal notranslate"><span class="pre">-system</span></code> or <code class="docutils literal notranslate"><span class="pre">-I</span></code> to the
CFLAGS. The path to this <code class="docutils literal notranslate"><span class="pre">math.h</span></code> must be defined last so that it has
precedence. This will include the NuttX <code class="docutils literal notranslate"><span class="pre">wint_t</span></code> definition, then continue
to include the default version of <code class="docutils literal notranslate"><span class="pre">math.h</span></code>.</p></li>
</ul>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef _MYMATH_H</span>
<span class="cp">#define _MYMATH_H</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;wchar.h&quot;</span>
<span class="cp">#include_next &lt;math.h&gt;</span>
<span class="cp">#endif </span><span class="cm">/* _MYMATH_H */</span>
</pre></div>
</div>
<ul class="simple">
<li><p>The PX4 team uses these patches to <a class="reference external" href="https://github.com/PX4/Firmware/blob/nuttx_v3/nuttx-patches/c%2B%2B11.patch">cwhar</a>
and <a class="reference external" href="https://github.com/PX4/Firmware/blob/nuttx_v3/nuttx-patches/math.h.patch">math.h</a>
to solve the issue. But note the comments in that code:</p></li>
</ul>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="w"> </span><span class="cm">/* N.B. The following definitions are enabled at this time to allow the PX4</span>
<span class="cm">* development to continue until there is a SAFE solution to foreign</span>
<span class="cm">* (non-nuttx) header file inclusion. There is a potential of a binary</span>
<span class="cm">* incompatibility and runtime errors, memory overwrites or corruption</span>
<span class="cm">* VVVVVVVVVVVVVVVVVVVVVVVVVV Begin Warning VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV</span>
<span class="cm">*/</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Some people have suggested adding the type definition of <code class="docutils literal notranslate"><span class="pre">wint_t</span></code> to
<code class="docutils literal notranslate"><span class="pre">nuttx/include/sys/types.h</span></code> merely because that header file will then
be included into the newlib <code class="docutils literal notranslate"><span class="pre">math.h</span></code>. This inclusion, of course, also
<cite>very dangerous</cite> since the types in the NuttX <code class="docutils literal notranslate"><span class="pre">sys/types.h</span></code> header file
may not agree with the types in the pre-compiled newlib math library.
This solution is not recommended, in any case. The type <code class="docutils literal notranslate"><span class="pre">wint_t</span></code> is
already correctly defined in <code class="docutils literal notranslate"><span class="pre">nuttx/include/sys/wchar.h</span></code> which is the
one and only correct location per
<a class="reference external" href="http://pubs.opengroup.org/onlinepubs/007908775/xsh/wchar.h.html">OpenGroup.org</a>.
It is a mystery to me why the newlib <code class="docutils literal notranslate"><span class="pre">math.h</span></code> header file uses <code class="docutils literal notranslate"><span class="pre">wint_t</span></code>
without including <code class="docutils literal notranslate"><span class="pre">wchar.h</span></code>. If it did, then there would then this
compilation issue would not exist
(there could still be subtle binary compatibility issues).</p></li>
<li><p>The ideal solution would be to integrate a third-party, optimized,
ARM math library into NuttX, building it using only NuttX header
files. That would guarantee no binary incompatibility and would be
a very useful contribution to NuttX.</p></li>
</ul>
<p>Changes to the <code class="docutils literal notranslate"><span class="pre">nuttx/arch/&lt;architecture&gt;/src/Makefile</span></code> may also be required so
that the linker can find and include the math library (similar to the existed
logic in the <code class="docutils literal notranslate"><span class="pre">Makefile</span></code> to find <code class="docutils literal notranslate"><span class="pre">libgcc.a</span></code>).</p>
<p>Update: This issue may have finally been resolved with this commit:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>commit 894ca622e6a408e5fa858a3fee46fb16f32cf86c
Author: Xiang Xiao \&lt;xiaoxiang@xiaomi.com\&gt;
Date: Mon Aug 27 06:26:37 2018 -0600
include/sys/types: Move wint_t and wctype_t from wchar.h to
types.h. This change is compatible as before since wchar.h
include types.h indirectly. This fixes a compilation error with
newlib&#39;s math.h: &#39;unknown type name wint_t&#39;
</pre></div>
</div>
</section>
<section id="cmath">
<h3>cmath<a class="headerlink" href="#cmath" title="Permalink to this heading"></a></h3>
<p>This error has been reported:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>/nuttx/include/cxx/cmath:124:11:<span class="w"> </span>error:<span class="w"> </span><span class="s1">&#39;::log2l&#39;</span><span class="w"> </span>has<span class="w"> </span>not<span class="w"> </span>been<span class="w"> </span>declared...
</pre></div>
</div>
<p>Apparently the function logic <code class="docutils literal notranslate"><span class="pre">log2l()</span></code> is defined in the NuttX <code class="docutils literal notranslate"><span class="pre">math.h</span></code> and
added to the <code class="docutils literal notranslate"><span class="pre">std::</span></code> namespace in <code class="docutils literal notranslate"><span class="pre">cmath</span></code>. But, apparently, the
newlib <code class="docutils literal notranslate"><span class="pre">math.h</span></code> does not prototype <code class="docutils literal notranslate"><span class="pre">lib2l()</span></code>.</p>
<p>If you plan to use the newlib <code class="docutils literal notranslate"><span class="pre">math.h</span></code> and the NuttX <code class="docutils literal notranslate"><span class="pre">cmath</span></code>, then
you probably have to modify <code class="docutils literal notranslate"><span class="pre">cmath</span></code> as well.</p>
</section>
<section id="alloca-h">
<h3>alloca.h<a class="headerlink" href="#alloca-h" title="Permalink to this heading"></a></h3>
<p>If your imported application includes <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code>, then you will run into the
same kinds of issues. Nuttx does not provide this header file an so you will
probably end up including the newlib version of <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code> which has similar
disastrous results.</p>
<p>One solution for GCC (only) would be to provide you own <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code> containing:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef _ALLOCA_H</span>
<span class="cp">#define _ALLOCA_H</span>
<span class="cp">#define alloca __builtin_alloca</span>
<span class="cp">#endif </span><span class="cm">/* _ALLOCA_H */</span>
</pre></div>
</div>
<p>And add the path to this <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code> to your <code class="docutils literal notranslate"><span class="pre">CFLAG</span></code> include path arguments.
The path can specified by adding <code class="docutils literal notranslate"><span class="pre">-system</span></code> or <code class="docutils literal notranslate"><span class="pre">-I</span></code> to the CFLAGS. The path
to this <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code> must be defined last so that it has precedence.</p>
<p>However, if you include newlib header files that include other <code class="docutils literal notranslate"><span class="pre">alloc.h</span></code>
you may still end up including the newlib <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code>. So another solution
might be to include your own <code class="docutils literal notranslate"><span class="pre">math.h</span></code>, for example, which contains
something like like:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifndef _MYMATH_H</span>
<span class="cp">#define _MYMATH_H</span>
<span class="cp">#ifndef _ALLOCA_H</span>
<span class="cp">#define _ALLOCA_H</span>
<span class="cp">#define alloca __builtin_alloca</span>
<span class="cp">#endif </span><span class="cm">/* _ALLOCA_H */</span>
<span class="cp">#include_next &lt;math.h&gt;</span>
<span class="cp">#endif </span><span class="cm">/* _MYMATH_H */</span>
</pre></div>
</div>
<p>This will provide the <code class="docutils literal notranslate"><span class="pre">alloca()</span></code> definition, then continue to include
the default version of <code class="docutils literal notranslate"><span class="pre">math.h</span></code>. This works because the idempotence
pre-processor variable <code class="docutils literal notranslate"><span class="pre">_ALLOC_H</span></code> matches the same idempotence variable
used in the newlib <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code>. Thus, any sneak inclusion of <code class="docutils literal notranslate"><span class="pre">alloca.h</span></code>
with have not effect.</p>
</section>
</section>
<section id="c-issues">
<h2>C++ Issues<a class="headerlink" href="#c-issues" title="Permalink to this heading"></a></h2>
<p>Most of the C++ issues that have not so much to do with header
files as with C++ name mangling and strict typing.</p>
<section id="new-operator">
<h3>new Operator<a class="headerlink" href="#new-operator" title="Permalink to this heading"></a></h3>
<p>The prototype for the C++ new operator is:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">operator</span><span class="w"> </span><span class="n">new</span><span class="p">(</span><span class="kt">size_t</span><span class="w"> </span><span class="n">nbytes</span><span class="p">)</span>
</pre></div>
</div>
<p>However, <code class="docutils literal notranslate"><span class="pre">size_t</span></code>, in turn, defined to be either <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code> or
<code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">int</span></code> in the toolchain. This differs with different
versions of GCC toolchains and has nothing to do with header
file inclusion. NuttX supports a configuration option to work
around this, change new to either:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#ifdef CONFIG_CXX_NEWLONG</span>
<span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">operator</span><span class="w"> </span><span class="n">new</span><span class="p">(</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">nbytes</span><span class="p">)</span>
<span class="cp">#else</span>
<span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">operator</span><span class="w"> </span><span class="n">new</span><span class="p">(</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">nbytes</span><span class="p">)</span>
<span class="cp">#endif</span>
</pre></div>
</div>
<p>This C++ name mangling issue has been around for years and varies from
GCC compiler to GCC compiler, apparently due to some <cite>newlib</cite>
configuration difference.</p>
</section>
<section id="uint32-t">
<h3>uint32_t<a class="headerlink" href="#uint32-t" title="Permalink to this heading"></a></h3>
<p>Similarly, you may find that the definition of <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> in NuttX may
be incompatible with your toolchain’s libraries. You may,
perhaps, see errors like:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>error:<span class="w"> </span>redeclaration<span class="w"> </span>of<span class="w"> </span><span class="s1">&#39;typedef long unsigned int std::uint_least32_t&#39;</span>
</pre></div>
</div>
<p>The definition in the ARM header file at <code class="docutils literal notranslate"><span class="pre">nuttx/arch/arm/include/types.h</span></code> is:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">signed</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_int32_t</span><span class="p">;</span>
<span class="k">typedef</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">_uint32_t</span><span class="p">;</span>
</pre></div>
</div>
<p>On ARM platforms, <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code> and <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">int</span></code> are both 32-bit
integers so it does not matter which definition you use. But it does matter
if you are concerned about the C++ name mangling associated with the use of
size_t by your compiler. If you see errors such as the above, then
you can replace these type definition to avoid C++ name mangling
incompatibilities like:</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">signed</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">_int32_t</span><span class="p">;</span>
<span class="k">typedef</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">_uint32_t</span><span class="p">;</span>
</pre></div>
</div>
<p>But this now may result in additional problems, now there may be
incompatibilities between definition of <code class="docutils literal notranslate"><span class="pre">size_t</span></code> uses in NuttX and the
definition of <code class="docutils literal notranslate"><span class="pre">size_t</span></code> used in the libraries.</p>
</section>
<section id="size-t">
<h3>size_t<a class="headerlink" href="#size-t" title="Permalink to this heading"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">size_t</span></code> should be an integer type wide enough to hold the size of the
largest memory object. So <code class="docutils literal notranslate"><span class="pre">size_t</span></code> really depends on the size of the
underlying pointer type. For CPUs with 16-bit addressing, for example,
the width of <code class="docutils literal notranslate"><span class="pre">size_t</span></code> should be 16-bits; for CPUs with 32-bit addressing,
the width should be 32-bits.</p>
<p><code class="docutils literal notranslate"><span class="pre">uint32_t</span></code>, of course, should always be 32-bits in width.</p>
<p>Using newlib header files, you also encounter incompatibilities between
the definitions of some types, the types <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> and <code class="docutils literal notranslate"><span class="pre">size_t</span></code> is often
the sources of problems. For example:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>error:<span class="w"> </span>redeclaration<span class="w"> </span>of<span class="w"> </span><span class="s1">&#39;typedef unsigned int std::size_t&#39;</span>
</pre></div>
</div>
<p>The root cause of this issue is that the community cannot decide on
the correct definition of <code class="docutils literal notranslate"><span class="pre">size_t</span></code>. NuttX uses this <cite>flexible</cite>
definition of <code class="docutils literal notranslate"><span class="pre">size_t</span></code>:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="kt">size_t</span><span class="p">;</span>
</pre></div>
</div>
<p>It is <cite>flexible</cite> in the sense that <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> is determined by architecture
specific header files, <cite>not</cite> the RTOS itself. That definition will be either
<code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">long</span></code> or <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">int</span></code>. So the <code class="docutils literal notranslate"><span class="pre">size_t</span></code> type compatibility
can differ with different compilers and also with different architectures
(NOTE that since <code class="docutils literal notranslate"><span class="pre">size_t</span></code> should be wide enough to hold the size of the
largest addressable object. <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> only works for 32-bit addressable
machines. Perhaps, <code class="docutils literal notranslate"><span class="pre">size_t</span></code> should really be defined as type <code class="docutils literal notranslate"><span class="pre">uintptr_t</span></code>?).</p>
<p>This can be fixed by changing the definition of <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> as described
above. But that could introduce <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> name mangling incompatibilities.
In that case, you have no option but to decouple the definition of <code class="docutils literal notranslate"><span class="pre">size_t</span></code>
from <code class="docutils literal notranslate"><span class="pre">uint32_t</span></code> by changing the definition in <code class="docutils literal notranslate"><span class="pre">nuttx/include/sys/types.h</span></code>
to:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="kt">size_t</span><span class="p">;</span>
</pre></div>
</div>
<p>or</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">size_t</span><span class="p">;</span>
</pre></div>
</div>
<p>whichever resolves the incompatibility.</p>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="ipv6.html" class="btn btn-neutral float-left" title="IPv6" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="protected_build.html" class="btn btn-neutral float-right" title="NuttX Protected Build" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2023, The Apache Software Foundation.</p>
</div>
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>