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

Makefile: Compiling C++

Details: SDK v15.2.0. Windows 8.1.

I am trying to adjust the ble_app_hts Makefile such that I can compile C++. I added cppmain.cpp to SRC_FILES and that threw me an error

_build/nrf52832_xxaa/cppmain.cpp.o: In function `idle_state_handle()':
c:\Users\Michael\Documents\rStrap\nRF5_SDK_15.2.0_9412b96\examples\ble_peripheral\ble_app_hts\pca10040\s132\armgcc/../../../Src/cppmain.cpp:27: undefined
reference to `nrf_pwr_mgmt_run()'

My cppmain.cpp looks like this

#include <cstdint>

#include "cppmain.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"


void cppmain()
{
    while(1)
    {
        idle_state_handle();
    }
}

/**@brief Function for handling the idle state (main loop).
 *
 * @details If there is no pending log operation, then sleep until next the next event occurs.
 */
void idle_state_handle(void)
{
    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }
}

I can remove this error by adding extern "C" before the declaration of  nrf_pwr_mgmt_run() but that's hacky. I'm surprised it even compiled tbh, I thought i would need to separate the SRC_FILES into CC/CXX/ASM but I noticed the .s in the list so I added my .cpp file. My next thought is to mix the Makefile with the one offered in this question, but if there is an easier way please let me know. I attached the Makefile (it shouldn't be called Makefile.makefile but just Makefile wouldn't upload).

Makefile.makefile

Parents
  • I can remove this error by adding extern "C" before the declaration of  nrf_pwr_mgmt_run() but that's hacky.

     That's because the nrf_pwr_mgmt_run() is a function from a library written in C that you refer to in a C++ file. That's how you're supposed to handle C code in a C++ project. Look at https://isocpp.org/wiki/faq/mixing-c-and-cpp.

     

    but if there is an easier way please let me know

     Not as far as I know.

Reply
  • I can remove this error by adding extern "C" before the declaration of  nrf_pwr_mgmt_run() but that's hacky.

     That's because the nrf_pwr_mgmt_run() is a function from a library written in C that you refer to in a C++ file. That's how you're supposed to handle C code in a C++ project. Look at https://isocpp.org/wiki/faq/mixing-c-and-cpp.

     

    but if there is an easier way please let me know

     Not as far as I know.

Children
  • Many libraries have extern "C" baked into each header file (ex: stm, tensorflow, 1). Purely out of curiosity, why did Nordic choose not to do this?

    #ifdef __cplusplus
     extern "C" {
    #endif
    
    ...code...
    
    #ifdef __cplusplus
     }
    #endif

    To wrap up the question "how do I adjust the SDK makefile so it compiles C++" the answer is, you don't have to do anything. If you add a .cpp file to the SRC_FILES list it compiles fine. Just wrap any C includes in extern "C". For example, in main.cpp:

    extern "C" {
    #include "nrf_pwr_mgmt.h"
    }

    Thanks for your response Hakon

Related