Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Problem with compiling C++ code for nRF52820

Hi,

I tried to build the ble_app_uart example for nRF52820/s140. In my project I need to use C++ modules.

My usual approach that worked so far:

  • Take an example project
  • Rename the main.c to main.cpp
  • Replace line 381-385
    ble_gap_phys_t const phys =
    {
        .tx_phys = BLE_GAP_PHY_AUTO,
        .rx_phys = BLE_GAP_PHY_AUTO,
    };
  • Compile with -std=gnu+11.

Oddly this doesn't work with the nRF52820 example and I always get the following error when compiling.

In file included from ../nrf5_sdk/modules/nrfx/mdk/nrf.h:192,
                 from D:\Development\wDALI2_BT_Switch\wdali-2-bt-button\firmware\ble_app_uart_pca10100e_s140\main.cpp:55:
../nrf5_sdk/modules/nrfx/mdk/compiler_abstraction.h:193:60: error: expected constructor, destructor, or type conversion before '(' token
../nrf5_sdk/modules/nrfx/mdk/compiler_abstraction.h:232:5: note: in expansion of macro 'NRF_STATIC_ASSERT'
../nrf5_sdk/modules/nrfx/mdk/nrf_erratas.h:48:1: note: in expansion of macro 'NRF_MDK_VERSION_ASSERT_AT_LEAST'

When I do the same thing with the ble_app_uart example for nRF52840/s140 it compiles just fine with C++.

Does anyone have an idea how to fix this problem and why it only occurs when compiling for nRF52820?

Thanks in advance.

Parents Reply Children
  • Right now I am just using the ble_app_uart for pca10100e/s140 example (nRF5 SDK 17.0.2) with no additions other than the following to make it build with the C++ compiler.

    • Rename the main.c to main.cpp
    • Replace line 381-385
       
      ble_gap_phys_t const phys =
      {
          .tx_phys = BLE_GAP_PHY_AUTO,
          .rx_phys = BLE_GAP_PHY_AUTO,
      };
    • Compile with -std=gnu+11.

    This is my edited nrf_erratas.h file: 

    nrf_erratas.h

    This is the compilation error I get:

    In file included from ../nrf5_sdk/modules/nrfx/mdk/nrf.h:192,
                     from D:\Development\wDALI2_BT_Switch\wdali-2-bt-button\firmware\ble_app_uart_pca10100e_s140\main.cpp:55:
    ../nrf5_sdk/modules/nrfx/mdk/compiler_abstraction.h:193:60: error: expected constructor, destructor, or type conversion before '(' token
    ../nrf5_sdk/modules/nrfx/mdk/compiler_abstraction.h:232:5: note: in expansion of macro 'NRF_STATIC_ASSERT'
    ../nrf5_sdk/modules/nrfx/mdk/nrf_erratas.h:52:1: note: in expansion of macro 'NRF_MDK_VERSION_ASSERT_AT_LEAST'

  • Can you try to comment out some lines in nrf_erratas.h to see whether it is one of the lines or one of the files from there that your compiler doesn't like? It may look like it is from compiler_abstraction.h. Perhaps you can try to add the cplusplus guart in that file as well. 

  • db_lw said:
    Replace line 381-385

     Those are the original lines. (except tx and rx is swapped). Are you sure you started with a clean example?

    Line 378 - 387 from the original main.c file:

            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                NRF_LOG_DEBUG("PHY update request.");
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);

  • I have added the extern C part to compiler_abstraction.h but it is still not working.

    Those are the three files I made changes to, to make it work with C++:

    2475.main.cpp

    compiler_abstraction.h

    6278.nrf_erratas.h

  • I still don't have the setup (IDE settings and so on) to replicate this. Is there any additional modifications from the SDK, apart from this? The compiler error is a quite generic error that you'll see when you add an '{' or '(' on the wrong place. 

    The fact that you mentioned the lines:

    ble_gap_phys_t const phys =
    {
        .tx_phys = BLE_GAP_PHY_AUTO,
        .rx_phys = BLE_GAP_PHY_AUTO,
    };

    Which are identical to the original example makes me wonder whether this comes from a clean SDK, or if someone else has handed you the SDK. Perhaps they already did some changes to make it compile for the other boards?

    BR,

    Edvin

Related