Skip to content

Commit 0693793

Browse files
committed
fix: update nuttx article for external static libraries
Apply some text changes for better reading. Signed-off-by: Filipe Cavalcanti <[email protected]>
1 parent de82822 commit 0693793

File tree

1 file changed

+28
-15
lines changed
  • content/blog/2025/11/nuttx-external-lib

1 file changed

+28
-15
lines changed

content/blog/2025/11/nuttx-external-lib/index.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
---
22
title: "Integrating External Libraries into NuttX Applications"
33
date: 2025-11-19
4+
lastmod: 2025-11-27
45
tags: ["NuttX", "Apache", "ESP32", "ESP32-C6", "RISC-V", "cross-compilation", "static-library"]
56
showAuthor: false
67
authors:
78
- "filipe-cavalcanti"
8-
summary: "This guide demonstrates how to integrate external libraries into NuttX applications using static libraries and cross-compilation. Learn how to build a library on x86, integrate it into the NuttX simulation environment, and cross-compile for RISC-V targets like the ESP32-C6, all without moving your codebase into the NuttX directory structure."
9+
summary: "This guide demonstrates how to integrate external libraries into NuttX applications using static libraries and cross-compilation. Learn how to build a library on x86, integrate it into the NuttX simulation environment, and cross-compile for RISC-V targets like the ESP32-C6, all without moving your entire codebase into the NuttX directory structure."
910
---
1011

1112
## Introduction
1213

13-
When moving your application to NuttX, you often need to add your existing software stack to the NuttX image. This software may run on a different RTOS or even on an x86 environment, but sometimes it must run on multiple target devices. This article shows how to build NuttX with your custom application without moving your entire stack to the NuttX directory. You use a static library and cross-compilation to achieve this.
14+
When moving your application to NuttX, you may need to add your existing codebase to the NuttX build environment. That is a common scenario where the user application contains mostly hardware-independent logic (such as data processing and analysis) that was originally developed on a POSIX compliant system. Those applications are usually tested on an x86 machine and adapted to run on an RTOS, or, in the case of NuttX, it should not need changes if the codebase is already POSIX compliant.
1415

15-
This article is divided into three parts. The first part introduces and builds the sample library on x86. Then, a second part decribes how to add the library to the NuttX simulation environment and finally, the last part cross-compiles to RISC-V and runs the example on the ESP32-C6.
16+
This article shows how to build NuttX with your custom application without moving your entire stack to the NuttX directory. You use a static library and cross-compilation to achieve this.
17+
18+
This article is divided into three parts. The first part introduces an example library and builds it on x86. Then, the second part decribes how to add the library to the NuttX simulation environment. The third part cross-compiles to RISC-V and runs the example on the ESP32-C6. Finally, the fourth part concludes the article.
1619

1720
## Using an example library
1821

19-
As an example, we will use an example application library that converts a hexadecimal color string to RGB with the following structure:
22+
For demonstration purposes, we will use an example application library that converts a hexadecimal color string to RGB with the following structure:
2023

2124
```
2225
hex-converter/
@@ -34,27 +37,34 @@ The reference project is available in [this repository](https://github.com/fdcav
3437
The `hex-converter` library exposes one single function called `hex_to_rgb`. The user provides a pointer to a string representing the hex color and a pointer
3538
to an array where the R, G, and B values are copied. It is a simple application but very useful as an example.
3639

37-
Clone the repository and build it according to the steps in the README file to produce the static library. To confirm that everything works, run the provided `main` example program. This program accepts a hexadecimal color string as input.
40+
Now, let's use the library on our x86 machine and see how it operates.
41+
42+
1. Clone the repository
43+
2. Build the library according to the steps in the README file
44+
3. Execute the provided `main` example program:
3845

3946
```bash
4047
$ ./main "#1A2B3C"
4148
Input: #1A2B3C
4249
RGB: (26, 43, 60)
4350
```
4451

45-
At this point, the directory should contain a static library called `libhex_to_rgb.a` that will be added to the NuttX build system.
52+
At this point, the directory should contain a static library called `libhex_to_rgb.a`.
4653

4754
## Testing on NuttX Simulation
4855

49-
As an user, you might want to use this library in an application. The first solution might be to copy the entire hex-converter repository to the NuttX application directory and
56+
As a user, you might want to use this library in an application. The first solution might be to copy the entire hex-converter repository to the NuttX application directory and
5057
add it entirely to the build system. That works but is complicated, not user-friendly, and causes a Makefile mess.
5158

59+
Before proceeding, if you are new to NuttX please read the [Getting Started with NuttX and ESP32](https://developer.espressif.com/blog/nuttx-getting-started/) which contains an introduction setting up NuttX.
60+
5261
The simplest way to test this library on NuttX is to modify the ready-to-use Hello World example in the NuttX Apps repository, which could in fact be any application.
5362

5463
With your NuttX environment ready, follow these steps:
5564

56-
1. Copy `libhex_to_rgb.a` from the hex-converter repository to `apps/examples/hello` (the Hello World example directory).
57-
2. In `apps/hello/Make.defs`, add the hex library, library path, and include path.
65+
1. Clone the example repository and build `libhex_to_rgb.a` as discussed in [Using an example library](#using-an-example-library)
66+
2. Copy `libhex_to_rgb.a` to `apps/examples/hello` (the Hello World example directory)
67+
3. In `apps/hello/Make.defs`, add the hex library, library path, and include path
5868

5969
The Make.defs file should look like this:
6070
```
@@ -131,19 +141,22 @@ nsh>
131141
132142
Success! We can compile our library externally, link it to a NuttX application, and use it.
133143
134-
## Using the library on ESP32C6
144+
Now that simulation works, let's look into a real use case that requires the same code to work on hardware.
135145
136-
Now that simulation works, we must look into a real use case that requires the same code to work on hardware.
137-
For this, we must compile the library to be supported on our RISC-V target.
146+
## Using the library on the ESP32C6
138147
139-
### Cross-compilation
148+
This section shows how to prepare the library for an actual target device—in this case, the ESP32-C6. To do this, we first cross-compile the example library for the RISC-V architecture and then integrate the resulting static library into the "Hello Example" that will be executed on
149+
our target.
140150
141-
In the hex-converter Makefile, the CC instruction changes to `riscv-none-elf-gcc` instead of `gcc` when you set the TARGET variable.
151+
### Cross-compilation
142152
143153
Clear the environment to delete the x86 build and rebuild for RISC-V:
154+
144155
1. `make clean`
145156
2. `make TARGET=riscv32`
146157
158+
In the hex-converter Makefile, the CC instruction changes to `riscv-none-elf-gcc` instead of `gcc` when you set the TARGET variable.
159+
147160
The same `libhex_to_rgb.a` library is ready, but now it can be used on RISC-V devices. This can be verified easily:
148161
149162
```
@@ -190,7 +203,7 @@ This article demonstrates how to integrate external libraries into NuttX applica
190203
191204
The static library approach offers several advantages. You can develop and test your code on an x86 machine without flashing the target device. The same library works across different architectures with minimal changes, requiring only a recompilation step. This workflow saves development time and simplifies the porting process.
192205
193-
By following these steps, you can add existing software stacks to NuttX without moving your entire codebase into the NuttX directory structure. This approach maintains separation between your application code and the RTOS, making maintenance and updates easier.
206+
By following these steps, you can add existing applications to NuttX without moving your entire codebase into the NuttX directory structure. This approach maintains separation between your application code and the RTOS, making maintenance and updates easier.
194207
195208
## Related Resources
196209

0 commit comments

Comments
 (0)