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

C++ Compilation Segger Embedded Studio Extern C

Greetings, 

I am relatively new to compilers and I would like some clarification on compiling C++ projects on Segger Embedded Studio with Nordic's SDK vs. 14.2.

I am use to programming in Arduino and I prefer object oriented programming in C++ compared to C and want to make custom c++ libraries for some I2C and SPI devices with the SDK.

I am working with the template project found in the peripheral folder of the SDK with SES. I changed the "main.c" to "main.cpp" to allow it to be compiled by a cpp compiler (C++11 compiler in SES rather than the default gnu++98) and compiles without any warning or errors. 

Question: How do I properly link the .c files found in the SDK with my project? Do I need to include the "__cplusplus" flag to my preprocessor?

I was given the impression that in order to properly link c and c++ files, the "extern "C" " as shown in the snippet below is important and is normally not active at the start of a project. How does this piece code affect the overall compiler and output files? 

#ifdef __cplusplus
extern "C" {
#endif

Thanks in advance!

Parents
  • Hi.

    Nordic doesn't have any official support for C++ in SES (or C++ in any other compiler), so I'm afraid you have to figure this out yourself. You should not need to include the "__cplusplus" flag in the preprosessor, as it should already be defined in the header files, if it is not, then you should add it. You should be able to use the C functions in C++.

    Best regards.

    Andreas

  • Well! That's an unpleasant surprise :-(

    DevZone peeps, anyone got this working?

    At the moment the Segger GCC is refusing to recognize _Static_assert, despite setting C Language standard to gnu11 and C++ to gnu++14

  • Some progress. Resolved _Static_assert by #ifdef'ing in the C++ 'static_assert' for the C++ case (app_util.h). Also had to add "extern c{}" to nrf_pwr_mgmt.h (SDK 14.2). Changed main.c to main.cpp in the ble_ant_app_hrm_pca10040_s332 example. Compiles and works. Added in some additional code (LCD display class). Code compiles and runs, but the display is not working and the debugger keeps hanging. Freezes up the whole PC. Need to shut off the dev kit and wait for something (?) to time out before it comes back.

Reply
  • Some progress. Resolved _Static_assert by #ifdef'ing in the C++ 'static_assert' for the C++ case (app_util.h). Also had to add "extern c{}" to nrf_pwr_mgmt.h (SDK 14.2). Changed main.c to main.cpp in the ble_ant_app_hrm_pca10040_s332 example. Compiles and works. Added in some additional code (LCD display class). Code compiles and runs, but the display is not working and the debugger keeps hanging. Freezes up the whole PC. Need to shut off the dev kit and wait for something (?) to time out before it comes back.

Children
Related