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

Compiling C and C++ code

We've currently only been developing our application in C. We now have a couple of C++ libraries we'd like to use and are having trouble compiling the mixture of C and C++ code.

This is the Makefile: http://pastebin.com/M4Wsybxt And this is the output when I run make: http://pastebin.com/4SmrvJuz

All the #include statements are wrapped in an extern "C" block.

The code that is causing the code to not compile seems to be this (from main.cpp file):

static void gpio_config() {
    ret_code_t err_code;
     
    // Initialize driver
    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);
     
    // Configure output pins for LEDs
    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    err_code = nrf_drv_gpiote_out_init(LED_RED, &out_config);
    APP_ERROR_CHECK(err_code);
    err_code = nrf_drv_gpiote_out_init(LED_BLUE, &out_config);
    APP_ERROR_CHECK(err_code);
     
    // Set output pins
    nrf_drv_gpiote_out_set(LED_RED);
    nrf_drv_gpiote_out_set(LED_BLUE);
     
    // Turn leds off
    nrf_drv_gpiote_out_toggle(LED_RED);
    nrf_drv_gpiote_out_toggle(LED_BLUE);
     
    /* No inputs for now */
}

It seems like the compiler might be ignoring the extern "C" statement, since I get the same error if I leave that out.

I'm currently using the arm-none-eabi-g++ compiler, but also tried arm-none-eabi-c++ with no difference.

Could anyone help me get on the right track to compiling this C and C++ code? I'd rather not have to change the NRF SDK code to make this compile.

Parents
  • You might try changing from an initializer to an assignment:

    nrf_drv_gpiote_out_config_t out_config;
    out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);
    

    You might treat this function as C: put it in a separate file and compile it as C, declare it as extern "C" in main and call it from your C++ main.

    These solutions don't require changing the SDK. You should not be afraid to change the SDK (but yes, it would require a redo when a new SDK comes out.)

    The SDK partially (not fully) supports C++ compilation (some #ifdef __cplusplus.) In my experience, you can (but it is not trivial) compile the SDK as C++ with just a few changes. The main thing that bit me was: if you define any IRQ handlers to override the default, make sure to declare then as extern "C" so the names aren't mangled. You can use the "nm" command to examine .o and .bin files to see what names are mangled.

  • Hi, I solved this issue at the time by going around the GPIOTE_CONFIG_OUT_SIMPLE macro. Instead just doing

    // Configure output pins for LEDs
    nrf_drv_gpiote_out_config_t out_config = {};
    out_config.init_state = NRF_GPIOTE_INITIAL_VALUE_LOW;
    out_config.task_pin = false;
    
Reply Children
No Data
Related