在C++中使用extern来共享vairables
Using extern in C++ to share vairables
我在网上看过使用extern的例子,但当我将其应用于我的项目时,它要么说变量在项目中定义了多次,要么说变量超出了的范围
main.cpp
#include <SimPre.h>
void setup() {
example();
simController.println("Test");
}
void loop() {
example();
}
SimPre.h
#include <SoftwareSerial.h>
#ifndef SIM_PRE
#define SIM_PRE
extern SoftwareSerial simController(7, 8);
void example();
#endif
SimPre.cpp
#include <Arduino.h>
#include "SimPre.h"
void example() {
simController.println("Test");
}
上面的代码显示我试图从main.c访问simController变量,它也应该可以从SimPre.c访问,但我得到了一个错误:
libraries/SimPre/SimPre.cpp.o:(.bss.simController+0x0): multiple definition of `simController'
sketch/DilshadSIM.ino.cpp.o:(.bss.simController+0x0): first defined here
collect2: error: ld returned 1 exit status
在SimPre.h中将其声明为:
extern SoftwareSerial simController;
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 在cuda线程之间共享大量常量数据
- 如何从具有移动语义的类对象中生成共享指针
- 在c代码之间共享数据的最佳方式
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 将静态库链接到不带-fPIC的共享库中
- 为什么std::互斥需要很长的、非常不规则的时间来共享
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 无法在Ubuntu上将共享库与Eclipse链接
- 从python调用openMP共享库时,未定义opnMP函数
- 在为LINUX创建共享库时,如何避免STL的私有/弱副本
- 为什么我的共享库中存在展开符号
- 使用共享指针的函数调用,其对象应为 const
- 具有两个独占锁组的共享锁
- 共享队列的线程安全
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 多个"常量引用"变量可以共享同一个内存吗?
- 如何访问由共享指针保存的类方法?
- 在C++中使用extern来共享vairables