1

At the moment I have a configuration file called "config.h". This file has multiple parameters that are retrieved by multiple header files.

Folder source structure under PlatformIO:

src/
├── main.cpp
├── Temperature.h
├── Temperature.cpp
├── config.h

main.cpp requests a configuration integer from the config.h file. temperature.h has the config.h included to get the address of a temperature sensor.

Now I get the error in the linker:

.pio\build\esp32dev\src\main.cpp.o:(.data.mode+0x0): multiple definition of `mode'
.pio\build\esp32dev\src\Temperature.cpp.o:(.data.mode+0x0): first defined here
.pio\build\esp32dev\src\main.cpp.o:(.bss.address+0x0): multiple definition of `address'
.pio\build\esp32dev\src\Temperature.cpp.o:(.bss.address+0x0): first defined here

I've tried setting Pragma once and header guards in the config.h but it didn't get any results.

config.h:

#ifndef CONFIG_H 
#define CONFIG_H
#pragma once

/* WiFi Settings */
#define WIFI_SSID                       "WiFiOnBusiness"
#define WIFI_PASSWORD                   "WiFiPassword"

/* Temperature BME280 Configuration (Keep as it) */
uint8_t address = 0;      
uint8_t mode = 3;             

#endif

What is the correct way to make sure to import the config file into multiple header files?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • Are these values constants or variables? – Alan Birtles Dec 08 '21 at 07:48
  • Do you want `mode` to be a global variable that both translations can edit? or do you want the two translation units to have separate copies of `mode`? – Elliott Dec 08 '21 at 07:49
  • mode should only be able to adjust from temperature.cpp (and readed). The WIFI SSD is only read by main – Jacco Veldscholten Dec 08 '21 at 07:54
  • See [this answer](https://stackoverflow.com/a/10422049/8658157). I'm not sure why the duplicate link is for nested includes, which isn't your problem. By the way, there's no need for `#pragma once` – Elliott Dec 08 '21 at 07:57
  • I proposed the duplicate because the rules explained there I am convinced to fix the problem here. OP wonders why using reinclusion guards does not help. That misunderstanding regularily causes multi-definition problems. And the code shown for config.h violates the rules and easily causes the mentioned errors. An [mre] would be needed to make sure. But I bet a dinner (I select the restaurant) that following the rules fixes this. – Yunnosch Dec 08 '21 at 08:06
  • mark constants with `const`, variables need to be defined in one cpp file and declared `extern` in headers – Alan Birtles Dec 08 '21 at 08:18
  • @Yunnosch, removing `#pragma once` doesn't solve the issue (and the OP implied they'd already tried that). The issue is that there's a variable definition in a header file (the answer I linked before explains it). So, I'll pick a restaurant! – Elliott Dec 09 '21 at 06:53
  • @Eliott What gave you the impression that I recommend to remove the pragma? What is unclear about "I pick the restaurant"? – Yunnosch Dec 09 '21 at 07:19

0 Answers0