C++ and GCC error while registering SoftDevice observers

Hi everyone,

I have a project written in C++ for nRF52 series using the SDK 15.3.

If I use Keil and uVIsion, the project compiles and works great. With the same source code, if I use VSCode and GCC, the project compiles but crashes at run time. I identify the problem but I do not find the way to solve it.

Nordic SDK uses MACRO to register SoftDevice observers. This macro is called: 'NRF_SDH_BLE_OBSERVER'. The first problem is that this MACRO can be used with C++98 (for Keil). So I wrote directly in C++ the corresponding code in my project. The code is the following:

#if defined(__CC_ARM)
    static nrf_sdh_ble_evt_observer_t _scannerBleObs __attribute__ ((section(STRINGIFY(CONCAT_2(sdh_ble_observers,
                     NRF_BLE_SCAN_OBSERVER_PRIO))))) __attribute__((used)) = {
#elif defined(__GNUC__)
    static nrf_sdh_ble_evt_observer_t _scannerBleObs __attribute__ ((section("." STRINGIFY(CONCAT_2(sdh_ble_observers,
                     NRF_BLE_SCAN_OBSERVER_PRIO))))) __attribute__((used)) = {

This code is used to register in 'sdh_ble_observersX' section of the flash, my observer which is needed to get events back from SoftDevice to my BLE scanner. With that code, both Keil compiler and GCC are happy and both compile my project without error or warning.

Now at runtime, with Keil, the software works great, the observer is correctly registered and my BLE scanner is notified  without fail by the SoftDevice when events are dedicated to it. The next pictures is a screenshot of Keil running in debug mode my software with a breakpoint just after the code above. You can see in that picture that the handler and the p_context of  _scannerBleObs are well defined, with correct pointer address.

Using GCC, the trick is very different ! In the next picture, which is a screenshot of vscode running my software in debug mode, you can see that the handler of my observer is set correctly, but the p_context is null !!!

As this context's pointer is NULL, my scanner is never notified by the SoftDevice.

  • What are the CXXFLAGS and LDFLAGS that Keil uses to make it work?
  • Has anyone ever encountered this problem before?
  • Does anyone have a solution for me?

S140 v6.1.1

Thanks for reading.