stm32 cube ide 使用 c 和 c++ 混合开发

需求 在使用 stm32 cube ide 开发的时候,希望通过 c++ 开发,但是自动生成的一些文件是 c 语言的,所以需要解决混合开发的问题。 解决 项目使用 cpp 在新建项目的时候,targeted language 选择 C++ 即可。 混合开发 所有 cpp 需要用到的 c 的头文件,都需要包含 extern "C" 这个标识,需要把相关代码如下包含起来: // may be main.h #ifdef __cplusplus extern "C" { #endif // something #ifdef __cplusplus } #endif 参考 STM32CubeIDE实现nRF24L01通信(C和C++混合编程) STM32CubeMX快速创建工程 点亮LED灯 设置C/C++混合编译 让你的 STM32Cube KEILV5 + HAL库工程支持C++开发 C 和 C++混合编译

2023-05-17 · 1 min · 56 words · RamLife

stm32 不用的引脚,应该配置为什么状态

需求 mcu 不使用的引脚,应该如何配置,才比较合适。 解决 低功耗目的: 模拟输入,外部悬空 普通抗干扰: 内部上下拉,外部悬空 最强抗干扰: 外部直接连接 GND 参考 STM32未使用引脚如何处理

2023-05-17 · 1 min · 13 words · RamLife

成员变量是引用,需要通过初始化列表来初始化

需求 类中包含的成员变量是引用,如何对这种引用成员变量进行初始化? 解决 这种引用成员变量初始化只能使用初始化列表。 class CommandProcess { public: CommandProcess(uint64_t& send_count); private: uint64_t& msg_send_count_; }; CommandProcess::CommandProcess(uint64_t& send_count) : msg_send_count_(send_count) { } 参考 C/C++ - 类中成员变量是引用

2023-05-10 · 1 min · 24 words · RamLife

weak_ptr 给构造函数引用参数必须是 const, 否则 error: cannot bind non-const lvalue reference of type to an rvalue of type

需求 A类掌握资源,使用 shared_ptr 来指向资源。其他 B,C,D 类使用资源,在构造函数中使用 weak_ptr 作为引用参数来接收资源指针。但是编译是会报错,报错信息: error: cannot bind non-const lvalue reference of type ‘std::weak_ptr<>&’ to an rvalue of type ‘std::weak_ptr<>’ 解决 解决办法很简单,在构造函数的参数 weak_ptr 前面加上 const. std::unique_ptr<CommandProcess> process_; std::shared_ptr<MessageQueue> send_; process_ = make_unique<CommandProcess>(send_); CommandProcess::CommandProcess(const weak_ptr<MessageQueue>& send) : send_(send) { } 参考 c++ 智能指针 传参 GotW #91 Solution: Smart Pointer Parameters C/C++面试:weak_ptr的使用场景 关于c ++:我应该通过引用传递shared_ptr吗? C++ shared_ptr 作为参数和返回值的比较 C++11:再谈shared_ptr,以及shared_ptr参数传递以及构造细节 C++非const引用问题:error: cannot bind non-const lvalue reference of type 【报错】关于{Error} cannot bind non-const lvalue reference of type ‘std::String&‘ to an rvalue……的一个解决方案...

2023-05-10 · 1 min · 81 words · RamLife

c++ error: is an inaccessible base of

需求 今天编译报错 error: is an inaccessible base of 解决 这个问题解决也非常简单,主要是默认的继承实际上是 private, 所以不能直接访问父类的成员,只要在继承时,用 public 进行标识即可。 class A { public: virtual int add(int a, int b); }; class B : public A { int add(int a, int b) { return a + b; } }; 参考 ‘A’ is an inaccessible base of ‘B’解决方案 C++ is an inaccessible base of 问题的解决方法

2023-05-10 · 1 min · 58 words · RamLife

c++ 子类构造初始化和父类构造初始化

需求 子类构造时会调用父类构造函数,具体如何匹配? 解决 父类 子类 匹配 null null 编译器默认生成 父类和子类的构造函数 null 无参 或 带参 调用编译器生成的父类构造函数 无参 没有显式调用父类构造函数 隐式调用父类无参构造函数 带参 必须显式调用父类构造函数 显式调用,否则编译会报错 带参且都有默认值 不用显式调用父类构造函数 可以隐式调用父类有默认值的带参构造函数 无参或带参 只需要实现父类构造函数中的任何一个即可 没有显式调用的情况下,默认调用父类无参构造函数 参考 c++ 子类构造函数初始化及父类构造初始化 C++子类构造函数初始化及父类构造初始化

2023-05-10 · 1 min · 31 words · RamLife

c++ struct 使用初始化列表无效

需求 今天使用统一初始化, {} 初始化 struct 之后,发现数据还是 0,没有初始化进去。 解决 经过网上查找之后,找到问题所在,列表初始化只能用于 aggregate, 而 class, struct, union 是否属于 aggregate, 需要满足以下几个条件: 无自定义构造函数 无私有或受到保护的非静态数据成员 无基类 无虚函数 类的定义中,不能有被 {} 和 = 直接初始化的非静态数据成员 // 自定义构造函数 struct Test { int x; int y; Test(int, int) {} }; // 保护非静态数据成员 struct Test { int x; int y; protected: int z; }; // 基类 struct base{}; struct Test : base { int x; int y; }; // 虚函数 struct Test { int x; int y; virtual void func(int, int) {} }; // 直接初始化 struct Test { int x; int y = 5; }; 所以只要注意以上几点即可,但是如果一定要这种情况下还要能使用列表进行初始化,也是可以的,需要如下定义:...

2023-04-27 · 1 min · 116 words · RamLife

shell 命令 tail 有什么作用

需求 linux shell 中的 tail 有啥作用? 解决 tail 是用来读最后的数据的,一般用来读取实时增长的 log,把最新的 log 给显示出来。 tail -f /etc/log.log 参考 Linux命令详解:tail - 实时查看文件内容 Linux tail 命令

2023-04-24 · 1 min · 22 words · RamLife

子类调用父类被重写的虚函数

需求 今天需要在子类的虚函数中,调用父类被重写的同名方法,来完成部分工作。 解决 this->Command::ToString(cmd) 类似上面这样,可以通过 this 指针直接拿到父类的方法 Command::ToString . 编译器可以自动找到并调用父类的方法。 参考 C++——子类调用父类方法 c++父类虚函数被子类虚函数覆盖后,如何直接调用父类的虚函数? C++ | 子类对象调用父类函数

2023-04-19 · 1 min · 16 words · RamLife

查看 g++ 生成的虚函数表

需求 为了了解父子类对于虚函数表的影响,需要查看编译器生成的虚函数表。 解决 生成包含虚函数表的文件。 # g++ 8 之前 g++ -fdump-class-hierarchy vptr.cpp # g++ 8 之后 g++ -fdump-lang-class vptr.cpp 编译后,后产生一个 *.class 的文件,查看这个文件就可以看到子类的虚函数表了。 参考 c++ 查看对象内存布局 C/C++杂记:深入虚表结构 从编译器的辅助信息看c++对象内存布局 C++在gcc下的单继承,多继承,虚继承的内存布局 如何查看c++的虚函数表 C++知识积累:如何获取虚函数表以及虚函数地址

2023-04-19 · 1 min · 29 words · RamLife