如何将库用于另一个库? [Arduino ESP32]

发布于 2025-02-04 01:17:41 字数 1617 浏览 5 评论 0原文

我正在研究Arduino ESP32的项目,并且有很多全球变量(用于数据生成)。我决定创建一个图书馆,以使我的工作更好一些。但是,我将此库用于其他用途的图书馆。编译后,我有以下错误:

sketch\OX2inj_LEVEL_OX2.cpp.o:(.data.addrChipId+0x0): multiple definition of `addrChipId'
sketch\First_Useage.cpp.o:(.data.addrChipId+0x0): first defined here
sketch\OX2inj_LEVEL_OX2.cpp.o:(.bss.ChipID+0x0): multiple definition of `ChipID'
sketch\First_Useage.cpp.o:(.bss.ChipID+0x0): first defined here

这是我的.ino(MAIN)代码:

#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
#include "First_Useage.h"

//---------somthing

void setup() 
{
  Serial.begin(115200);
  //---------somthing
  Serial.println(ChipID.ReadStrEEPROM());
  //---------somthing

}

void loop() 
{
  //---------somthing
}

这是我的“ def_global_var.h”代码

#ifndef Def_Global_Var_H
#define Def_Global_Var_H

#include "Var_Str_EEPROM.h"


uint16_t addrChipId = 1;

VarStrEEPROM ChipID(addrChipId);

#endif

,这是我的“ first_useage” 。

 #ifndef First_Usage_H
#define First_Usage_H

void getchipid();

#endif

#include "First_Useage.h"
#include <Arduino.h>

#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"

void getchipid()
{
  uint32_t chipId = 0;
  for(int i=0; i<17; i=i+8) 
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  ChipID.WriteStrEEPROM(String(chipId));
}

​程序认为:“我在呼唤图书馆”,并且看到它以前被调用了,它不喜欢它。 它有点正确吗?如果是(或不正确),我该怎么办?

编辑:对不起,我把错误的部分放了。现在已经纠正了

I am working on a project on Arduino ESP32 and I have a lot of Global variables (for data generation). I have decided to create a library in order to orgenise my work a little better. But I use this library into other librari's that I had to create for other usage. after compilation it I have the following error :

sketch\OX2inj_LEVEL_OX2.cpp.o:(.data.addrChipId+0x0): multiple definition of `addrChipId'
sketch\First_Useage.cpp.o:(.data.addrChipId+0x0): first defined here
sketch\OX2inj_LEVEL_OX2.cpp.o:(.bss.ChipID+0x0): multiple definition of `ChipID'
sketch\First_Useage.cpp.o:(.bss.ChipID+0x0): first defined here

here is my .ino (main) code :

#include <Arduino.h>
#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"
#include "First_Useage.h"

//---------somthing

void setup() 
{
  Serial.begin(115200);
  //---------somthing
  Serial.println(ChipID.ReadStrEEPROM());
  //---------somthing

}

void loop() 
{
  //---------somthing
}

here is my "Def_Global_Var.h" code

#ifndef Def_Global_Var_H
#define Def_Global_Var_H

#include "Var_Str_EEPROM.h"


uint16_t addrChipId = 1;

VarStrEEPROM ChipID(addrChipId);

#endif

here is my "First_Useage.h" code

 #ifndef First_Usage_H
#define First_Usage_H

void getchipid();

#endif

here is my "First_Useage.cpp" code :

#include "First_Useage.h"
#include <Arduino.h>

#include "Var_Str_EEPROM.h"
#include "Def_Global_Var.h"

void getchipid()
{
  uint32_t chipId = 0;
  for(int i=0; i<17; i=i+8) 
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  ChipID.WriteStrEEPROM(String(chipId));
}

My understanding is that, when I use the #include "Def_Global_Var.h", the programme thinks that : "I am calling the library" and it sees that it has been called before and it does not like it.
Is it somehow correct ? and if it is(or not) correct what should I do?

EDIT : sorry I have put the wrong part of the prog. it has been corrected now

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2025-02-11 01:17:41

实际原因是将标头包含在几个源文件中,因此您最终会在.o文件中对这些变量的多个相互冲突的定义。

您通常不应该在标题文件中定义全局变量;您应仅应声明将它们作为extern

#ifndef Def_Global_Var_H
#define Def_Global_Var_H
...
extern uint16_t addrChipId;
...
#endif

第二步是在相应的.cpp文件中定义变量,这次没有extern关键字:

// Def_Global_Var.cpp

uint16_t addrChipId = 1;

由于def_global_var.o仅链接一次,因此不再有冲突。

The actual cause is that the header is included into several source files, so you end up with multiple conflicting definitions of these variables in your .o files.

You shouldn't normally define global variables in header files at all; you should only declare them as extern:

#ifndef Def_Global_Var_H
#define Def_Global_Var_H
...
extern uint16_t addrChipId;
...
#endif

The second step is to define the variable in the corresponding .cpp file, this time without the extern keyword:

// Def_Global_Var.cpp

uint16_t addrChipId = 1;

Since Def_Global_Var.o gets linked only once, there should be no more conflicts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文