This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Programming nRF52 DK in C++ in Segger Embedded Studio

Hi.

I am writing software for my custom nRF52832 device but hardware wise it is identical to the nRF52 DK. 

I'm not that good of an embedded coder (or any kind of coder for that matter) and I am trying to integrate some i2c sensors on my device. 

But all the libraries of these sensors are written in C++, and are using objects and classes. Which C doesn't support.

I have managed to convert *some* of the code (in probably a very inefficient way) to plain C, but most of the code is beyond my capabilities. 

How can I code with C++ on this thing using Segger Embedded Studio? 

Any help would be greatly appreciated. 

- Usman

Parents
  • Hi Usman

    Any .cpp file should automatically be compiled using the C++ compiler, while any .c files will be compiled by the C compiler, so it should be possible to mix and match C and C++ code. 

    There might be library files in the SDK that will not compile in C++, because they use language features only available in C, but it should be possible to get around this by using C++ for your sensor libraries only.

    Have you tried to integrate any .cpp files in your project?

    If yes, did you face any issues?

    Best regards
    Torbjørn 

  • Hi Torbjørn, thanks for pointing that out.

    When I compile, it gives this error where the object class is defined

    unknown type name 'class'

    What am I missing here?
    Regards
    Usman

  • Hi Usman

    Did you move all the instance definitions at the top of main.c into the separate file? 

    They should look something like this:

    BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);                                   /**< BLE NUS service instance. */
    NRF_BLE_GATT_DEF(m_gatt);                                                           /**< GATT module instance. */
    NRF_BLE_QWR_DEF(m_qwr);                                                             /**< Context for the Queued Write module.*/
    BLE_ADVERTISING_DEF(m_advertising);                                                 /**< Advertising module instance. */
    

    These macros will not compile in C++, and they will also have to be available to the rest of the BLE code if you move this into a separate file. 

    Best regards
    Torbjørn

  • Hi Torbjørn

    Yes, I tried that as well. But no good.

    And so I tried to compile them with just printf( ) in it and here's what my files look like:

    main.cpp:

    #include "c_file.h"
    
    int main(void)
    {
        func();
    }

    c_file.h:

    #ifndef CFile_H
    #define CFILE_H
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    
    int func();
    
    #endif 

    c_file.c:

    #ifndef CFile_C
    #define CFILE_C
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    
    int func()
    {
        printf("this c file");
        return 0;
    }
    
    #endif 

    And it gives me this error: undefined reference to `func()'

    Am I doing something wrong here? 

    Regards

    Usman

  • Hi Usman

    Good point, I forgot to mention this. Every header file that defines functions that you need to include in a C++ file needs to use the extern "C" header, otherwise they won't be visible to the C++ linker. 

    If you open any of the standard header files in the SDK you can spot this. 

    Essentially they will have the following somewhere at the top of the header file (after any includes but before any defines or function prototypes): 

    #ifdef __cplusplus
    extern "C" {
    #endif

    And then the following at the bottom of the file (before the final #endif):

    #ifdef __cplusplus
    }
    #endif

    Best regards
    Torbjørn

  • Hi Torbjørn

    After a few attempts, I finally managed to get this working

    And so I tried to compile them with just printf( )

    and it compiled.

    So I put in my original code in these files, and now it returns the same old error. 

    expected constructor, destructor, or type conversion before '(' token

    How could it be that it is compiling the simple .c file inside the .cpp file perfectly, but as soon as I add my other code to the .c file, it fails to compile? The my code in original .c file itself was compiling and working perfectly. 

  • Hi Usman

    Are you able to share your project with me so I can try it out?

    I can make the case private if you don't want to share it in public. 

    Best regards
    Torbjørn

Reply Children
Related