使用外部定义并防止重复定义
Use of extern and preventing duplicate definitions
我正在用我正在使用的实际文件重新措辞(但大大剥离了。我需要在半睡半醒时退出发帖。
测试.hpp 内容如下:
/*
* test.hpp
*
* Created on: Apr 1, 2019
* Author: Mike
*/
#ifndef INCLUDE_TEST_HPP_
#define INCLUDE_TEST_HPP_
#include <U8x8lib.h>
#define R1 13
#define RGROUND 12 //the rotary switch is connected via header pins on the board for development.
#define R2 14
#define SWITCH 27
#define SCL 15
#define SDA 4
#define OLED_RESET 16
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <RotaryEncoder.h>
#include "OneButton.h"
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
OneButton button(SWITCH, true);
RotaryEncoder encoder(R1, R2);
void testFunc();
#endif /* INCLUDE_TEST_HPP_ */
/* end of test.hpp" */
测试.cpp读作:
#include <Arduino.h>
#include "test.hpp"
#include <U8x8lib.h>
#include <RotaryEncoder.h>
#include "OneButton.h"
void setup() {
u8x8.begin();
u8x8.clearDisplay();
// encoder.begin(); //there's no begin functions for either of these.
// button.begin();
}
void loop() {
encoder.tick();
button.tick();
u8x8.print("starting loop");
testFunc();
}
测试2.cpp读取
/*
* test2.cpp
*
* Created on: Apr 1, 2019
* Author: Mike
*/
#include "test.hpp";
void testFunc(){
encoder.getPosition();
//do some other stuff.
}
编译上述内容会给我以下错误:
Linking .pioenvsunofirmware.elf
.pioenvsunosrctest2.cpp.o (symbol from plugin): In function `u8x8':
(.text+0x0): multiple definition of `u8x8'
.pioenvsunosrctest.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pioenvsunosrctest2.cpp.o (symbol from plugin): In function `u8x8':
(.text+0x0): multiple definition of `button'
.pioenvsunosrctest.cpp.o (symbol from plugin):(.text+0x0): first defined here
.pioenvsunosrctest2.cpp.o (symbol from plugin): In function `u8x8':
(.text+0x0): multiple definition of `encoder'
.pioenvsunosrctest.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pioenvsunofirmware.elf] Error 1
如果我尝试使用 extern 添加三行
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
extern OneButton button(SWITCH, true);
extern RotaryEncoder encoder(R1, R2);
到 test.hpp 和 test2.cpp 文件,然后进行测试.cpp没有 extern,我收到此错误:
In file included from srctest.cpp:2:0:
include/test.hpp:34:46: warning: 'u8x8' initialized and declared 'extern'
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
include/test.hpp:35:24: warning: 'button' initialized and declared 'extern'
extern OneButton button(SWITCH, true);
^
include/test.hpp:36:29: warning: 'encoder' initialized and declared 'extern'
extern RotaryEncoder encoder(R1, R2);
^
srctest.cpp:8:39: error: redefinition of 'U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8'
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
In file included from srctest.cpp:2:0:
include/test.hpp:34:42: note: 'U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8' previously declared here
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
srctest.cpp:9:17: error: redefinition of 'OneButton button'
OneButton button(SWITCH, true);
^
In file included from srctest.cpp:2:0:
include/test.hpp:35:18: note: 'OneButton button' previously declared here
extern OneButton button(SWITCH, true);
^
srctest.cpp:10:22: error: redefinition of 'RotaryEncoder encoder'
RotaryEncoder encoder(R1, R2);
^
In file included from srctest.cpp:2:0:
include/test.hpp:36:22: note: 'RotaryEncoder encoder' previously declared here
extern RotaryEncoder encoder(R1, R2);
^
In file included from srctest2.cpp:8:0:
include/test.hpp:34:46: warning: 'u8x8' initialized and declared 'extern'
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
include/test.hpp:35:24: warning: 'button' initialized and declared 'extern'
extern OneButton button(SWITCH, true);
^
include/test.hpp:36:29: warning: 'encoder' initialized and declared 'extern'
extern RotaryEncoder encoder(R1, R2);
^
srctest2.cpp:10:46: warning: 'u8x8' initialized and declared 'extern'
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
srctest2.cpp:10:46: error: redefinition of 'U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8'
In file included from srctest2.cpp:8:0:
include/test.hpp:34:42: note: 'U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8' previously declared here
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
^
srctest2.cpp:11:24: warning: 'button' initialized and declared 'extern'
extern OneButton button(SWITCH, true);
^
srctest2.cpp:11:24: error: redefinition of 'OneButton button'
In file included from srctest2.cpp:8:0:
include/test.hpp:35:18: note: 'OneButton button' previously declared here
extern OneButton button(SWITCH, true);
^
srctest2.cpp:12:29: warning: 'encoder' initialized and declared 'extern'
extern RotaryEncoder encoder(R1, R2);
^
srctest2.cpp:12:29: error: redefinition of 'RotaryEncoder encoder'
In file included from srctest2.cpp:8:0:
include/test.hpp:36:22: note: 'RotaryEncoder encoder' previously declared here
extern RotaryEncoder encoder(R1, R2);
^
*** [.pioenvsunosrctest.cpp.o] Error 1
*** [.pioenvsunosrctest2.cpp.o] Error 1
[ERROR] Took 3.51 seconds
将这三个放入没有 extern 的 test.hpp 中,然后将它们放入 test.cpp 和 test2.cpp 中使用 extern 会给我几乎相同的错误。
你想要一个在标题中带有extern
的声明。只是宣言。所以在测试中
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
成为
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8;
强制性标准报价
当您extern
贴在上面并离开初始化时,
// bad code! Do not use!
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8( ... );
^ initialization
该extern
被有效地忽略,您将获得定义而不是声明。编译器可能会警告您这一点,它确实
include/test.hpp:34:46: warning: 'u8x8' initialized and declared 'extern'
但是,如果您不知道自己在寻找什么,则该消息将无济于事。无论如何,不要忽视警告。他们是编译器告诉你,虽然编译的东西(语法正确),但它可能并不意味着你想要什么或做你想做什么(它可能在逻辑上不正确)。您可能会发现警告通常包含比实际错误更有用的信息,因此请找出它们的含义并消除它们。
回到正题,
extern U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8;
在test.hpp中,承诺u8x8
已经或将在其他地方定义,并且可以安全使用。下一步信守承诺。在测试中.cpp XOR 测试2.cpp(两者之一,而不是两者)添加
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/SCL, /* data=*/SDA, /* reset=*/OLED_RESET);
对button
和encoder
做同样的事情。
更多阅读
何时在C++中使用外部
和 C,不是C++,但 C 和 C++ 在这里足够接近,因此这个更长的讨论很有用:如何使用 extern 在源文件之间共享变量?
相关文章:
- 具有外部"c"和程序集的未定义函数
- C++编程从外部文本文件定义数组大小
- 良好做法:如何定义用于编译的外部库的路径
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 模板类外部的长定义的替代项
- 使用带有 CMake 和 Conan 的外部库的未定义引用
- 可视化C++外部定义内联函数
- 具有外部定义的默认特殊成员函数
- C++ 为什么在定义的编译和链接之前引用外部实例的程序
- 删除 PCL 可视化工具中定义的 3D 框外部的点
- 在成员函数外部封闭类的定义中需要默认成员初始值设定项
- 为什么在类外部(但在头文件中)定义的类成员函数必须内联?
- 如何在命名空间中声明外部全局,然后定义它?
- 使用静态变量未解析的外部符号/未定义的引用时出错
- 自定义对象构造函数在循环外部循环
- 我可以有一个从外部不可见但未在标头中定义的静态友元函数吗?
- 创建类时已定义的未解析外部符号
- 定义外部时未解析的外部符号"struct API Api"
- 为将来的'unset'变量定义外部变量是否是一种好的做法?
- 在同一头文件中定义外部变量