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

use c++ for nrf51 in keil

hello there i want to use c++ library in nrf51dk project. i am using sdk12. i change main.c to main.cpp but i am getting error.uart_original.rar

i have uploaded code here. In abc.cpp file i declared like

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"

#ifdef __cplusplus
}
#endif

but i am getting compiler error. can you tell me how to solve it??

Parents
  • Hi

    All you have to do to allow C++ in a file is to configure that file as a C++ file (renaming it to .cpp is not strictly necessary):
    image description

    As for your code example, why are you including the .cpp files from main.cpp? The .h files should be included, and the .cpp files should be added to your code project.

    Looking at the code your issues don't seem to be C++ related as much as related to linking issues (not including the right files at the right places).

    Best regards
    Torbjørn

  • For clarity, are you using SDK v12.0.0, or one of the newer versions? My response is based on the assumption that you use v12.0.0.

    One issue is that you don't include function prototypes in abc.h, which means your main file is not able to find those functions. In other words you have to add the function prototypes like this:

    void uart_init();
    int uart_return();
    

    Secondly there appears to be an issue in bsp.h where the C++ include guard is located in the wrong place. To fix this find the following section:

    #ifdef BSP_UART_SUPPORT
    #include "app_uart.h"
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    #endif // BSP_UART_SUPPORT
    

    And change it to this:

    #ifdef BSP_UART_SUPPORT
    #include "app_uart.h"
    
    #endif // BSP_UART_SUPPORT
    
    #ifdef __cplusplus
    extern "C" {
    #endif    
    

    By making those changes I was able to get your code to compile.

Reply
  • For clarity, are you using SDK v12.0.0, or one of the newer versions? My response is based on the assumption that you use v12.0.0.

    One issue is that you don't include function prototypes in abc.h, which means your main file is not able to find those functions. In other words you have to add the function prototypes like this:

    void uart_init();
    int uart_return();
    

    Secondly there appears to be an issue in bsp.h where the C++ include guard is located in the wrong place. To fix this find the following section:

    #ifdef BSP_UART_SUPPORT
    #include "app_uart.h"
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    #endif // BSP_UART_SUPPORT
    

    And change it to this:

    #ifdef BSP_UART_SUPPORT
    #include "app_uart.h"
    
    #endif // BSP_UART_SUPPORT
    
    #ifdef __cplusplus
    extern "C" {
    #endif    
    

    By making those changes I was able to get your code to compile.

Children
No Data
Related