C++ Support Issue in nRF connect SDk issue

Hi,

We are facing some issues in merging c++ file in nCS 1.7.0

we are adding main.cpp in a project and we are copyig everything in main.c and executing the following modifications done in project

1. In "C Library Implementation section" --> Enabled "Newlib C library"
2. In "Sub Systems and OS Services section" --> Enabled "C++ support for the application"
3. In "project build.emProject" (opening in text editor) we're adding desired includes in the main.cpp file we add just as below:
<file file_name="main.cpp">
<configuration
Name="Common"
c_only_additional_options=
c_preprocessor_definitions=
c_system_include_directories=
c_user_include_directories=
code_completion_options=
cpp_only_additional_options=


but we get an error of "bluetooth/bluetooth.h: No such file or directory" it is not picking up .h include, we're excluding main.c from build as we have added main.cpp in project and copied same include directories of main.c in main.cpp in "project build.emProject" file
can you please suggest us if there is anything we can do in merging this cpp file

  • Hi 

    One thing to note if you want to use C++ with the nRF Connect SDK is that not all libraries support C++ fully, including bluetooth.h which uses some struct initializers that the C++ compiler does not support. 

    In order to use C++ with these libraries you need to have a separate C file that interfaces the library directly, and then access this C file from your C++ files. 

    To illustrate this I made a quick modification to the peripheral_hr example where I moved all the Bluetooth functionality into a separate C file, and implemented a small class in main.cpp to access it:

    277456_peripheral_hr_cpp.zip

    All I changed in the configuration was to include the CONFIG_CPLUSPLUS=y configuration in prj.conf

    I didn't uses Segger Embedded Studio (SES) to write this code, but you should be able to import the project in SES if you want. 

    Best regards
    Torbjørn

  • Hi Torbjørn,

    Thank you for quick response and sharing sample solution.

    "Hi, we also found that when we replaced (removed from build) main.c with main.cpp, the execution is not entering "int main( )" function of main.cpp, is it because main.cpp has "int main( )" rather than "void main( )" as in main.c, that the execution is not entering "int main( )". how to make changes so that execution enters "int main( )" of main.cpp..?"

    Note: in main.cpp, it is giving build error if i write void main(), hence using int main( )

    Regards,

    Hitesh

  • Hi Hitesh

    Were you able to get my example running? 

    It uses void main(void), just like the normal C examples, and I didn't have any issues with it. 

    Are you able to share your example with me so I can try to build it here?

    Best regards
    Torbjørn

  • Hi Torbjørn.

    We tried executing the files you shared it is not building based on the methods recommended 

    We're using similar configs and editing CMakeLists.txt

    #

    # Copyright (c) 2020 Nordic Semiconductor ASA

    #

    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause

    #

    cmake_minimum_required(VERSION 3.20.0)

    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

    project(NONE)

    # NORDIC SDK APP START

    FILE(GLOB app_sources ../babelfish/Code/uC/nRF52xxx/*.cpp)

    FILE(GLOB app_includes ../babelfish/Code/uC/nRF52xxx/*.h)

    FILE(GLOB app_Interface_sources ../Nordic_SDK/Interface_Files/*.c)

    FILE(GLOB app_Interface_includes ../Nordic_SDK/Interface_Files/*.h)

    target_sources(app PRIVATE

        src/main.cpp

        src/model_handler.c

        src/chat_cli.c

        ${app_sources}

        ${app_Interface_sources})

    target_include_directories(app PRIVATE include ${app_includes} ${app_Interface_includes})

    zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth include)

    # NORDIC SDK APP END

    Regards,

    Hitesh

Related