Skip to content

[BUG] error in building add_subdirectory for nuttx #19697

Closed

Description

@zyy37

Description / Steps to reproduce the issue

For the convenience of project expansion, I placed nuttx in a third-party library and set it in the following directory ~/dev/proj/third_party/nuttx. When I imported nuttx into the main project's CMakeLists.txt through add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/third_party/nuttx") to build, I found an error. The command is cd ~/dev/proj && cmake -B build -DBOARD_CONFIG=/home/ubuntu/dev/proj/third_party/nuttx/boards/sim/sim/sim/configs/nsh. It may be a problem with your path handling, such as fixed writing, etc. For example, your build directory is ~/dev/proj/build, and the 375th line of your CMakeLists.txt is written as set(ENV{KCONFIG_CONFIG} ${CMAKE_BINARY_DIR}/.config), which should be CMAKE_CURRENT_BINARY_DIR. Most projects can be built through add_subdirectory, I hope you can fix it. Thank you.

On which OS does this issue occur?

[OS: Linux]

What is the version of your OS?

24.04.1-Ubuntu

NuttX Version

7fc26f

Issue Architecture

[Arch: simulator]

Issue Area

[Area: Build System]

Host information

No response

Verification

  • I have verified before submitting the report.

Activity

  1. added
    Type: Bug
    on Aug 5, 2026
  2. added
    Arch: simulator
    OS: Linux
    on Aug 5, 2026
  3. zyy37 commented on Aug 8, 2026

    @zyy37
    Author

    @acassis Are you a PM? Could you check it, please? Thank you.

  4. acassis commented on Aug 8, 2026

    @acassis
    Contributor

    @zyy37 please take a look at #19755

    This is just an initial fix to prove that "nuttx sim" can work as external project. For real boards more modifications will be necessary by CMake experts (@simbit18)

  5. raiden00pl commented on Aug 9, 2026

    @raiden00pl
    Member

    cmake in nuttx was never designed with add_subdirectory support in mind. How would this even work with the current nuttx build flow?

  6. zyy37 commented on Aug 9, 2026

    @zyy37
    Author

    @zyy37 please take a look at #19755

    This is just an initial fix to prove that "nuttx sim" can work as external project. For real boards more modifications will be necessary by CMake experts (@simbit18)

    Very good, thank you very much. Because third-party libraries are usually added using add_subdirectory, which is also a community style, such as GitHub projects like Google Test and UORB. Here is the instruction manual for another project. Using add_subdirectory is also a category of out-of-tree building advocated in your document. If not, it's just a bit troublesome for oneself, but it can still be used.

    Image

    In addition, the reason why nuttx is placed in the third-party folder is also due to considerations for cross platform development of projects such as Linux, otherwise the directory tree would be very ugly. In addition, defining the path of the board with an explicit path may also be a good choice, while reducing ambiguity and your construction judgment.

  7. zyy37 commented on Aug 9, 2026

    @zyy37
    Author

    CMAKE_SOURCE_DIR: Refers to the top-level source code directory (i.e. the directory specified when executing cmake initially, usually containing the root CMakeLists.txt).
    CMAKE_CURRENT_SOURCE_DIR: Refers to the directory where the current processing location is located, named after the CMakeLists.txt file. When using add_subdirectory() to enter a subdirectories, its value will change to the path of that subdirectories.

    Similarly, there are corresponding variables for building directories:
    CMAKE_BINARY_DIR: Refers to the top-level build directory (i.e. the build directory specified when executing cmake, which corresponds to the root CMakeLists.txt).
    CMAKE_CURRENT_BINARY_DIR: Refers to the build directory corresponding to the current source code directory. In subdirectories, it is usually CMAKE_BINARY_DIR relative path of subdirectories.
    Simply put:

    Variables without CURRENT always point to the root position of the project.
    The variable with CURRENT points to the current location of CMakeLists.txt.

  8. reopened this on Aug 9, 2026
  9. acassis commented on Aug 9, 2026

    @acassis
    Contributor

    Let's reopen it, since the PR only fixes external building of sim, it doesn't support cross-compiling

  10. Swatantra-66 commented on Oct 21, 2026

    @Swatantra-66
    Contributor

    Hi @acassis,

    I have submitted a PR to fix cross-compilation support in add_subdirectory() mode for non-sim architectures: #20874

    It fixes inverted add_subdirectory() syntax, relative includes in Toolchain.cmake, subproject toolchain evaluation, and exports configuration variables to PARENT_SCOPE. Please take a look when you have a chance

  11. added 2 commits that reference this issue on Oct 21, 2026
    f4512b8
    96574aa
  12. added a commit that references this issue on Oct 26, 2026
    1fb5dd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: simulatorArea: Build systemOS: LinuxType: Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions