Arm Mbed OS support forum

How to add header files and source files?

question

How to add header files and source files?

detail

I am trying to write some programs using Mbed studio, but the default structure of Mbed OS is like this:

.
|-- BUILD
|   `-- NUCLEO_F103RB
|-- main.cpp
|-- mbed-os
|   |-- CONTRIBUTING.md
|   |-- DOXYGEN_FRONTPAGE.md
|   |-- Jenkinsfile
|   |-- LICENSE-apache-2.0.txt
|   |-- LICENSE.md
|   |-- README.md
|   |-- TESTS
|   |-- TEST_APPS
|   |-- UNITTESTS
|   |-- cmsis
|   |-- components
|   |-- docs
|   |-- doxyfile_options
|   |-- doxygen_options.json
|   |-- drivers
|   |-- events
|   |-- features
|   |-- hal
|   |-- logo.png
|   |-- mbed.h
|   |-- platform
|   |-- requirements.txt
|   |-- rtos
|   |-- targets
|   `-- tools
`-- mbed-os.lib

I want to modify the directory structure to this:

.
|-- BUILD
|   `-- NUCLEO_F103RB
|-- include
|-- lib
|   `-- pwm_sample
|-- mbed-os
|   |-- CONTRIBUTING.md
|   |-- DOXYGEN_FRONTPAGE.md
|   |-- Jenkinsfile
|   |-- LICENSE-apache-2.0.txt
|   |-- LICENSE.md
|   |-- README.md
|   |-- TESTS
|   |-- TEST_APPS
|   |-- UNITTESTS
|   |-- cmsis
|   |-- components
|   |-- docs
|   |-- doxyfile_options
|   |-- doxygen_options.json
|   |-- drivers
|   |-- events
|   |-- features
|   |-- hal
|   |-- logo.png
|   |-- mbed.h
|   |-- platform
|   |-- requirements.txt
|   |-- rtos
|   |-- targets
|   `-- tools
|-- mbed-os.lib
|-- src
|   `-- main.cpp
`-- test

I want to put the app driver I wrote in the lib directory, and put the main.cpp I wrote in the src directory, instead of putting all the files in the root directory, otherwise the structure of the code is very messy. But how should I add the src, lib, test, include directories to the project? I know keil can set include path, but Mbed Studio does not have instructions in this regard.

Hi @scarleast,

This is a good question. Mbed Studio supports it. It promotes having libraries in a program root folder but you can always move it to a new folder. You can do the same with main.cpp and other application source files.

How to do it:

  1. You can add a new folder by either:
  • Right-click the program in the file tree and select New Folder
  • Menu->File->New Folder
  1. Move library or application source code files into a new folder

Mbed Studio should detect the library path change and it should still be visible on Libraries panel. Build system should also detect correctly that source code is in different folder.

Note: You can move all libraries except mbed-os. Mbed OS library should always be in a program root folder.

Here is an example of the structure with main.cpp and library in it’s own folders:

1 Like

Thank you for your answer, very detailed, and really solved my question.