You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/03/22 21:01:53 UTC

[GitHub] [incubator-nuttx] fraviofii opened a new pull request #3138: Addind CMake based C++ project example in documentation

fraviofii opened a new pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138


   ## Summary
   
   The objective of this pull request is to provide an application example of a C++ project running outside the NuttX source tree.
   
   The motivation for this documentation is to provide a starting for developers implement C++ based applications using a previously defined NuttX configuration without the need to use the NuttX build structure.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598647494



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::

Review comment:
       ```suggestion
   .. code-block:: cmake
   ```

##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block::

Review comment:
       ```suggestion
   .. code-block:: cmake
   ```

##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block::
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block::
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block::
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>
+
+    extern "C"
+    {
+            int phigw_main(void)
+            {
+                    Log::print("PHI: Exemplo em CMake");
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block::
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block::    

Review comment:
       ```suggestion
   .. code-block:: c++
   ```

##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block::
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block::

Review comment:
       ```suggestion
   .. code-block:: cmake
   ```

##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block::
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block::
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block::

Review comment:
       ```suggestion
   .. code-block:: c++
   ```

##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,274 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake (https://www.cmake.org) build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block::
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block::
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block::
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block::
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>
+
+    extern "C"
+    {
+            int phigw_main(void)
+            {
+                    Log::print("PHI: Exemplo em CMake");
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block::

Review comment:
       ```suggestion
   .. code-block:: c++
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598734062



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       ```suggestion
               std::printf("Constructor: mSecret=%d\n",mSecret);
       }
   
       CHelloWorld::~CHelloWorld() {
   
       }
   
       bool CHelloWorld::HelloWorld(void) {
               std::printf("HelloWorld: mSecret=%d\n",mSecret);
   
               std::string sentence = "Hello";
               std::printf("TEST=%s\n",sentence.c_str());
   
               if (mSecret == 42) {
                       std::printf("CHelloWorld: HelloWorld: Hello, world!");
                       return true;
               }
               else {
                       std::printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
                       return false;
   ```
   Sorry being to picky on this, but it may fail to compile due to the missing namespace.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598718746



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,283 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>

Review comment:
       ```suggestion
   ```
   This is also not required in `main.cpp`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804280529


   @fraviofii please squash your patch into one:
   ```
   git rebase --interactive HEAD~18...HEAD
   ``
   and select the squash from terminal.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598720216



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,283 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )

Review comment:
       ```suggestion
       project(HelloCpp
               VERSION 1.0
               DESCRIPTION "Hello world C++ Nuttx"
       )
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis closed pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
acassis closed pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] dagar commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
dagar commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804379099


   Can I gauge if there's any interest in native cmake support for NuttX at this time? I think it would greatly improve integration with 3rd party projects, cross platform development support, and developmer productivity in general (much faster out of tree builds, etc). 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804061244


   > I have also added an introduction paragraph indicating the motivation for this document.
   > 
   > Are these changes enough?
   
   Can you try to use the `:caption:` I mentioned?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804099966


   > > > I have also added an introduction paragraph indicating the motivation for this document.
   > > > Are these changes enough?
   > > 
   > > 
   > > Can you try to use the `:caption:` I mentioned?
   > 
   > I added, but the code block content is gone.
   
   Strange. Ok, maybe better to revert that then.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598734062



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       ```suggestion
               std::printf("Constructor: mSecret=%d\n",mSecret);
       }
   
       CHelloWorld::~CHelloWorld() {
   
       }
   
       bool CHelloWorld::HelloWorld(void) {
               std::printf("HelloWorld: mSecret=%d\n",mSecret);
   
               std::string sentence = "Hello";
               std::printf("TEST=%s\n",sentence.c_str());
   
               if (mSecret == 42) {
                       std::printf("CHelloWorld: HelloWorld: Hello, world!\n");
                       return true;
               }
               else {
                       std::printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
                       return false;
   ```
   Sorry being so picky on this, but it may fail to compile due to the missing namespace.
   And also there is a missing `\n` at one printf.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598752105



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       If the C++ library is correctly configured, it shouldn't be a problem.
   `std::printf` is the C++ STL version, which is basically a wrapper for the C language version.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis edited a comment on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
acassis edited a comment on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804391874


   > Can I gauge if there's any interest in native cmake support for NuttX at this time? I think it would greatly improve integration with 3rd party projects, cross platform development support, and developmer productivity in general (much faster out of tree builds, etc).
   
   Hi @dagar,will this CMake offer work as an option to our original Makefiles instead of replacing it? The current build system works fine and CMake could be a tool of preference of some, but it will be a new tool for many developers. Some projects like OpenOCD tried to move from Makefiles to CMake many years ago and it never happened. Makefile is a consolidated tool.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804546136


   Yes, it's a big change: we have more than 200+ boards, 20000+ source files and 10+ external libraries. It's better to create a new issue to summarize the propose and let the community to dissuss and vote.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] fraviofii commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
fraviofii commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804838478


   > 
   > 
   > @fraviofii please squash your patch into one:
   > 
   > ```
   > git rebase --interactive HEAD~18...HEAD
   > ``
   > and select the squash from terminal.
   > ```
   
   @xiaoxiang781216 , it is done.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] fraviofii commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
fraviofii commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804089596


   > 
   > 
   > > I have also added an introduction paragraph indicating the motivation for this document.
   > > Are these changes enough?
   > 
   > Can you try to use the `:caption:` I mentioned?
   
   I added, but the code block content is gone.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] dagar commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
dagar commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804425444


   > Hi @dagar,will this CMake offer work as an option to our original Makefiles instead of replacing it?
   
   If there was enough interest in the NuttX community I'd propose doing it in parallel in order to fully support everything on all platforms. Then once at feature parity we'd either fully accept it or not. In my experience trying to support two things that do roughly the same thing is a good way to frustrate people and end up with multiple mediocre options.
    
   > The current build system works fine and CMake could be a tool of preference of some, but it will be a new tool for many developers.
   
   It's not that kind of tool where it can be left to individual preference, it either works accurately describing how to build the project or it doesn't. The part that can be developer preference is it can generate Makefiles, or Ninja build, or an IDE project. The support would roughly map 1-1 (Make.defs -> CMakeLists.txt per folder), but will ultimately be a bit different.
   
   > Some projects like OpenOCD tried to move from Makefiles to CMake many years ago and it never happened. Makefile is a consolidated tool.
   
   I don't know the history there, but there are plenty of other projects that made the transition (LLVM, Zephyr, PX4, etc). It may be a new tool for some, but it's also the closest thing to a standard build system in the C or C++ world and it solves a ton of headaches with cross platform development.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598715058



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"

Review comment:
       ```suggestion
   ```
   Not necessary if the Log class won't be used.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] fraviofii commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
fraviofii commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598749730



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       Won't it be a problem with NuttX's printf?
   
   Some functions, like printf, seems to be better used not from the language/compiler standard library, but from the platform. That's why I left the printf and stdio.h (and the testcxx example too ...).
   
   In standard C++ project, in general, printf is not used. But I believe in NuttX it is better to use.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis removed a comment on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
acassis removed a comment on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804388707


   > Can I gauge if there's any interest in native cmake support for NuttX at this time? I think it would greatly improve integration with 3rd party projects, cross platform development support, and developmer productivity in general (much faster out of tree builds, etc).
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598710920



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.

Review comment:
       ```suggestion
   applications using C++ language and also the cmake build tool.
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598734062



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       ```suggestion
               std::printf("Constructor: mSecret=%d\n",mSecret);
       }
   
       CHelloWorld::~CHelloWorld() {
   
       }
   
       bool CHelloWorld::HelloWorld(void) {
               std::printf("HelloWorld: mSecret=%d\n",mSecret);
   
               std::string sentence = "Hello";
               std::printf("TEST=%s\n",sentence.c_str());
   
               if (mSecret == 42) {
                       std::printf("CHelloWorld: HelloWorld: Hello, world!\n");
                       return true;
               }
               else {
                       std::printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
                       return false;
   ```
   Sorry being to picky on this, but it may fail to compile due to the missing namespace.
   And also there is a missing `\n` at one printf.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598716131



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>

Review comment:
       ```suggestion
   ```
   <iostream> not needed in this `main.cpp` file.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598717667



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>
+
+    extern "C"
+    {
+            int phigw_main(void)
+            {
+                    Log::print("PHI: Exemplo em CMake");
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>

Review comment:
       ```suggestion
       #include <cstdio>
   ```
   nit: Since this is a C++ example, how about sticking with the functions from the standard library?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804067829


   Please squash all commits once you are done adding changes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804391874


   > Can I gauge if there's any interest in native cmake support for NuttX at this time? I think it would greatly improve integration with 3rd party projects, cross platform development support, and developmer productivity in general (much faster out of tree builds, etc).
   
   Hi @dagar,will this CMake offer work as an option to our original Makefiles instead of replacing it? The current build system works fine and CMake could be a new tool of preference of some, but it will be a new tool for many developers. Makefile is a consolidated tool.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] fraviofii commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
fraviofii commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804059485


   I have also added an introduction paragraph indicating the motivation for this document.
   
   Are these changes enough?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598752105



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       If the C++ library is correctly configured, it shouldn't be a problem.
   `std::prinf` is the C++ STL version, which is basically a wrapper for the C language version.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598752105



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,300 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake build tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(HelloCpp
+            VERSION 1.0
+            DESCRIPTION "Hello world C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    :caption: stm32f4discovery.cmake
+    :name: stm32f4discovery.cmake
+    
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    :caption: CMakeLists.txt
+    :name: CMakeLists.txt
+   
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: main.cpp
+    :name: main.cpp
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include <nuttx/config.h>
+
+    extern "C"
+    {
+            int hellocpp_main(void)
+            {
+
+                    CHelloWorld *pHelloWorld = new CHelloWorld();
+                    pHelloWorld->HelloWorld();
+
+                    CHelloWorld helloWorld;
+                    helloWorld.HelloWorld();
+
+                    delete pHelloWorld;
+                    return 0;
+            }
+    }
+
+* hellocpp/src/HelloWorld.h
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.h
+    :name: HelloWorld.h
+
+    #ifndef HELLOWORLD_H_
+    #define HELLOWORLD_H_
+
+    #include "nuttx/config.h"
+
+    class CHelloWorld
+    {
+            public:
+                    CHelloWorld();
+                    ~CHelloWorld();
+                    bool HelloWorld(void);
+            private:
+                    int mSecret;
+    };
+
+    #endif
+
+* hellocpp/src/HelloWorld.cpp
+
+.. code-block:: c++
+    :language: c++
+    :caption: HelloWorld.cpp
+    :name: HelloWorld.cpp
+
+    #include <cstdio>
+    #include <string>
+
+    #include "HelloWorld.h"
+
+    CHelloWorld::CHelloWorld() {
+            mSecret = 42;
+            printf("Constructor: mSecret=%d\n",mSecret);
+    }
+
+    CHelloWorld::~CHelloWorld() {
+
+    }
+
+    bool CHelloWorld::HelloWorld(void) {
+            printf("HelloWorld: mSecret=%d\n",mSecret);
+
+            std::string sentence = "Hello";
+            printf("TEST=%s\n",sentence.c_str());
+
+            if (mSecret == 42) {
+                    printf("CHelloWorld: HelloWorld: Hello, world!");
+                    return true;
+            }
+            else {
+                    printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n");
+                    return false;

Review comment:
       If the C++ library is correctly configured, it shouldn't be a problem.
   `std::printf` is the version from the C++ STL implementation, which is basically a wrapper for the C language version.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598713668



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>
+
+    extern "C"
+    {
+            int phigw_main(void)

Review comment:
       ```suggestion
               int hellocpp_main(void)
   ```
   Just to be consistent with the aforementioned Application entry point




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804006696


   Thanks for adding this. Eventually it will be best to organize the guides section into subsections so it does not become a mess, but we can do that later on.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598714366



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>
+
+    extern "C"
+    {
+            int phigw_main(void)
+            {
+                    Log::print("PHI: Exemplo em CMake");

Review comment:
       ```suggestion
   ```
   This log line may be omitted from this example for the sake of simplicity.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] acassis commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
acassis commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804388707


   > Can I gauge if there's any interest in native cmake support for NuttX at this time? I think it would greatly improve integration with 3rd party projects, cross platform development support, and developmer productivity in general (much faster out of tree builds, etc).
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] gustavonihei commented on a change in pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
gustavonihei commented on a change in pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#discussion_r598716131



##########
File path: Documentation/guides/cpp_cmake.rst
##########
@@ -0,0 +1,287 @@
+.. include:: /substitutions.rst
+.. _cpp_cmake:
+
+C++ Example using CMake
+=======================
+
+In some situations, developers intend to implement software using the NuttX platform in
+a previously set hardware and configuration where it is not possible or allowed to make
+changes. In such situations, less contact with the operating source tree is better, where
+it is only used for the application.
+
+Some approaches are possible to do that today:
+
+* https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree
+* https://www.programmersought.com/article/61604062421/
+
+We have been seen the increase of the use of C++ language in embedded systems application. And
+CMake (https://www.cmake.org) is the preferred build system used to build C++ projects. NuttX
+support C++ based projects.
+
+Using the 'build as a library' procedure of NuttX, it is possible to build NuttX
+applications using C++ language and also the cmake  build
+tool.
+
+This document will show how to reimplement the hellocpp project using this cmake.
+
+Preparation
+-----------
+
+#. Base NuttX compilation changes
+
+    For this example, load the configuration 'stm32f4discovery:testlibcxx' for building
+
+    .. code-block:: console
+
+       $ cd nuttx
+       $ ./tools/configure.sh stm32f4discovery:testlibcxx
+
+    In menuconfig, the main points to be changed on a typical NuttX configuration are the following:
+
+    * Set RTOS Features -> Tasks and Scheduling -> Application entry point to 'hellocpp_main'
+
+    * Build NuttX and generate the export 
+
+    .. code-block:: console
+
+       $ make export
+
+Creating the project
+--------------------
+
+#. Create your project file structure
+
+    The project structure is organized as follow:
+
+    .. code-block:: console
+
+       hellocpp/
+       hellocpp/CMakeLists.txt
+       hellocpp/cmake/stm32f4discovery.cmake
+       hellocpp/nuttx-export-10.0.1/
+       hellocpp/src/CMakeLists.txt
+       hellocpp/src/main.cpp
+       hellocpp/src/HelloWorld.h
+       hellocpp/src/HelloWorld.cpp
+
+    The directory 'nuttx-export-10.0.1' is the unzipped content from the file created during
+    make export procedure done before.
+
+#. File contents
+
+* hellocpp/CMakeLists.txt
+
+.. code-block:: cmake
+
+    cmake_minimum_required(VERSION 3.2...3.15)
+
+    project(phigw
+            VERSION 1.0
+            DESCRIPTION "Hello world Phi-GW C++ Nuttx"
+    )
+
+    set(CMAKE_CXX_STANDARD 17)
+    set(CMAKE_CXX_STANDARD_REQUIRED ON)
+    # set(CMAKE_CXX_EXTENSIONS OFF)
+    set(CMAKE_C_STANDARD 99)
+
+    set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-10.0.1")
+
+    include(cmake/stm32f4discovery.cmake)
+
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow -Wundef -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer -Os")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -D_DEBUG -D_LIBCPP_BUILD_STATIC -D_LIBCPP_NO_EXCEPTIONS ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-exceptions -fcheck-new -fno-rtti -pedantic ")
+    set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -nostdinc++")
+
+    set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN")
+
+    include_directories(
+            src
+            ${NUTTX_PATH}/include
+            ${NUTTX_PATH}/include/libcxx
+            ${NUTTX_PATH}/arch/chip
+    )
+
+    set(EXE_NAME hellocpp)
+
+    set(CMAKE_CXX_FLAGS     "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} ${AC_CXX_EXTRA_FLAGS}")
+    if (PARAM_DEBUG)
+        set(CMAKE_CXX_FLAGS     "${CMAKE_CXX_FLAGS} -g")
+    endif()
+
+    set(CMAKE_SKIP_RPATH ON)
+    set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>")
+
+    set(BUILD_SHARED_LIBS OFF)
+
+    add_subdirectory(src)
+
+* hellocpp/cmake/stm32f4discovery.cmake
+
+.. code-block:: cmake
+    set(CMAKE_SYSTEM_NAME Generic)
+    set(CMAKE_SYSTEM_PROCESSOR arm)
+
+    set(MCU_LINKER_SCRIPT "${NUTTX_PATH}/scripts/ld.script")
+
+    set(COMPILER_PREFIX arm-none-eabi-)
+
+    # cmake-format: off
+    set(CMAKE_C_COMPILER    ${COMPILER_PREFIX}gcc)
+    set(CMAKE_CXX_COMPILER  ${COMPILER_PREFIX}g++)
+    set(CMAKE_AR            ${COMPILER_PREFIX}ar)
+    set(CMAKE_RANLIB        ${COMPILER_PREFIX}ranlib)
+    set(CMAKE_LINKER        ${COMPILER_PREFIX}ld)
+    set(CMAKE_ASM_COMPILER  ${COMPILER_PREFIX}gcc)
+    set(CMAKE_OBJCOPY       ${COMPILER_PREFIX}objcopy)
+    set(CMAKE_OBJDUMP       ${COMPILER_PREFIX}objdump)
+    set(CMAKE_SIZE          ${COMPILER_PREFIX}size)
+
+    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+    set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
+
+    set(AC_HW_FLAGS         "-mcpu=cortex-m4 -mthumb -mfloat-abi=soft ")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -isystem ${NUTTX_PATH}/include")
+    set(AC_HW_FLAGS         "${AC_HW_FLAGS} -pipe -D__NuttX__")
+
+    set(AC_LINKER_FLAGS     "--entry=__start -nostartfiles -nodefaultlibs -T${MCU_LINKER_SCRIPT}")
+
+* hellocpp/src/CMakeLists.txt
+
+.. code-block:: cmake
+    set(HEADER_FILES
+            HelloWorld.h
+    )
+
+    set(SOURCE_FILES
+            HelloWorld.cpp
+    )
+
+    link_directories(${EXE_NAME} ${NUTTX_PATH}/libs)
+
+    add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES})
+
+    add_custom_command(
+            TARGET ${EXE_NAME}
+            POST_BUILD
+            COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin
+    )
+
+    target_link_libraries(${EXE_NAME} --start-group)
+
+    target_link_libraries(${EXE_NAME} sched)
+    target_link_libraries(${EXE_NAME} drivers)
+    target_link_libraries(${EXE_NAME} boards)
+    target_link_libraries(${EXE_NAME} c)
+    target_link_libraries(${EXE_NAME} mm)
+    target_link_libraries(${EXE_NAME} arch)
+    target_link_libraries(${EXE_NAME} xx)
+    target_link_libraries(${EXE_NAME} apps)
+    target_link_libraries(${EXE_NAME} fs)
+    target_link_libraries(${EXE_NAME} binfmt)
+    target_link_libraries(${EXE_NAME} board)
+    target_link_libraries(${EXE_NAME} gcc)
+    target_link_libraries(${EXE_NAME} supc++)
+
+    target_link_libraries(${EXE_NAME} --end-group)
+
+* hellocpp/src/main.cpp
+
+.. code-block:: c++
+
+    #include <stdio.h>
+    #include "HelloWorld.h"
+    #include "Log.h"
+    #include <nuttx/config.h>
+    #include <iostream>

Review comment:
       ```suggestion
   ```
   `<iostream>` not needed in this `main.cpp` file.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] v01d commented on pull request #3138: Addind CMake based C++ project example in documentation

Posted by GitBox <gi...@apache.org>.
v01d commented on pull request #3138:
URL: https://github.com/apache/incubator-nuttx/pull/3138#issuecomment-804512944


   @dagar I think that we could discuss this maybe in a separate issue (maybe you can write a short proposal?) so we don't derail this PR. That said, I would be interested in this effort as I think nowadays cmake is quite standard and is worth the attempt. It could really simplify the build process, maintenance of the build system and speed (the ninja generator is a blazingly fast). Of course there are pros and cons, but maybe seeing the solution working in practice and actually trying to do it would go a long way to see what we think of it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org