This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

replacing main.c with main.cpp

Hello,

I am starting to work on a sample project, wherein I need to replace the existing "main.c" file I have with "main.cpp", by copying the contents of "main.c" into "main.cpp".
Can you please list me the steps I will be required to do to achieve the same.?

I am using nRF Connect extension on VScode.

Parents
  • Hi

    I recently made a small example for another customer (regarding this case), showing how to use C++ in an nRF Connect SDK Bluetooth sample. 

    In order to include .cpp files in your build all you have to do is make sure .cpp files are included in the CMakeLists.txt file, as I did with this line:

    FILE(GLOB app_sources src/*.c src/*.cpp)

    With that change in place it should be sufficient just to rename your main.c file to main.cpp. 

    The full example (for v1.6.1) is here: 

    3683.277456_peripheral_hr_cpp.zip

    Best regards
    Torbjørn

  • Hello ,

    Thanks for the suggestion, I am actually dependent on segger for my development, and the course of evaluation, I found the following issue:

    1. I started with a simple hello_world sample nCS app on segger embedded studio.

    2. Configured and enabled CONFIG_CPLUSPLUS=y

    3. Replaced main.c into main.cpp by copying contents of main.c into main.cpp

    4. Modified the CMakeLists.txt to include main.cpp as in:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world)
    #src/main.c
    target_sources(app PRIVATE D:/CPP_Test/cpp_files/main.cpp)


    5. And ended up with this error in main.cpp:

    Kindly suggest and request your help for a way out of this on segger.

    Have attached the project For your reference

    .CPP_Test.7z

  • Hi 

    I just tried to import the project I sent you in Segger, and it built without any problems. 

    You shouldn't use the extern "C" keyword around the include statements, like shown in the screenshot. The extern "C" keyword should only be used in the header files themselves, and all the standard Zephyr include files already have this statement. 

    If you look at my example you will see that I only use this statement in the app_bluetooth.h file. 

    Best regards
    Torbjørn

Reply
  • Hi 

    I just tried to import the project I sent you in Segger, and it built without any problems. 

    You shouldn't use the extern "C" keyword around the include statements, like shown in the screenshot. The extern "C" keyword should only be used in the header files themselves, and all the standard Zephyr include files already have this statement. 

    If you look at my example you will see that I only use this statement in the app_bluetooth.h file. 

    Best regards
    Torbjørn

Children
No Data
Related