RT-Thread 使用C++

      网络上讲RT-Thread 是C 语言编写的,但是应用程序可以使用C++ 来开发。但是网络上好像找不到说明和例子。只能自己测试:

    1 使用RT-Thread studio 开发工具

    2 在RT-thread Settings 中勾选 C++ 特性支持。支持 libc。

    3  项目中的所以程序必须使用cpp和hpp 后缀。当判别到cpp时,studio 才会使用g++ 编译器编译C++,要不然连class 都不认识。特别是main.c 要改成main.cpp

main.cpp

/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-09-02     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"
#include <stdio.h>
#define LED_PIN GET_PIN(I, 8)
class functionblock {
public:
    void function (int a);

private:
};
void functionblock::function(int a){
    printf("hello\n");
}

int main(void)
{
    rt_uint32_t count = 1;

    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
   functionblock fb;
   fb.function(100);
    while(count++)
    {
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
    }
    return RT_EOK;
}

#include "stm32h7xx.h"
static int vtor_config(void)
{
    /* Vector Table Relocation in Internal QSPI_FLASH */
    SCB->VTOR = QSPI_BASE;
    return 0;
}
INIT_BOARD_EXPORT(vtor_config);


也可以将类定义放在外边

Add.hpp

class Add {
public:
    int sum(int a,int b);
private:
};

Add.cpp

#include "Add.hpp"
int  Add::sum(int a,int b){
    return a+b;
}

main.cpp

/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-09-02     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_common.h"
#include <stdio.h>
#include "Add.hpp"
#define LED_PIN GET_PIN(I, 8)


int main(void)
{
    rt_uint32_t count = 1;

    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
  Add add;
  printf("sum(a,b) =%d\n", add.sum(100,2));
    while(count++)
    {
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_PIN, PIN_LOW);
    }
    return RT_EOK;
}

#include "stm32h7xx.h"
static int vtor_config(void)
{
    /* Vector Table Relocation in Internal QSPI_FLASH */
    SCB->VTOR = QSPI_BASE;
    return 0;
}
INIT_BOARD_EXPORT(vtor_config);


调试过的例子分享给你,希望帮助到同样困惑的朋友

猜你喜欢

转载自blog.csdn.net/yaojiawan/article/details/120180542