Error when importing file into other

Hi,

I have a project with the structure defined as in this image.

The file Aircon_def.h has some constants I want to use on the ui_events.c

#include <Arduino.h>

struct controlInfo {
  String ret;
  String power;
  String mode; //0 - fan. 1 - heat 2 - cool 3 - auto 7 - dry
  String temperature;
  String fanRate; // 1 - level 1  3 - level 2   5 - level 3
  String shum;
  String f_dir;
};

const int FAN = 0;
const int HEAT = 1;
const int COLD = 2;
const int AUTO = 3;
const int DRY = 7;


const int numberOfZones = 8;

const String ON = "1";
const String OFF = "0";

struct zonesStatusStruct{
  String zoneStatus[8];
  String zoneNames[8];
};

The project compiles fine, but when I include Aircon_def.h on ui_events.c it throws the error below:

uilding in release mode

warning: Calling missing SConscript without error is deprecated.
Transition by adding must_exist=False to SConscript calls.
Missing SConscript 'support/sdl2_build_extra.py'
File "/Users/eduardo/.platformio/penv/lib/python3.10/site-packages/platformio/builder/main.py", line 195, in <module>
Compiling .pio/build/esp8266/src/AirconnController.ino.cpp.o
Compiling .pio/build/esp8266/src/ui/ui_events.c.o
In file included from src/ui/ui_events.c:8:
src/ui/../Aircon_def.h:4:3: error: unknown type name 'String'
    4 |   String ret;
      |   ^~~~~~
src/ui/../Aircon_def.h:5:3: error: unknown type name 'String'
    5 |   String power;
      |   ^~~~~~
src/ui/../Aircon_def.h:6:3: error: unknown type name 'String'
    6 |   String mode; //0 - fan. 1 - heat 2 - cool 3 - auto 7 - dry
      |   ^~~~~~
src/ui/../Aircon_def.h:7:3: error: unknown type name 'String'
    7 |   String temperature;
      |   ^~~~~~
src/ui/../Aircon_def.h:8:3: error: unknown type name 'String'
    8 |   String fanRate; // 1 - level 1  3 - level 2   5 - level 3
      |   ^~~~~~
src/ui/../Aircon_def.h:9:3: error: unknown type name 'String'
    9 |   String shum;
      |   ^~~~~~
src/ui/../Aircon_def.h:10:3: error: unknown type name 'String'
   10 |   String f_dir;
      |   ^~~~~~
src/ui/../Aircon_def.h:22:7: error: unknown type name 'String'
   22 | const String ON = "1";
      |       ^~~~~~
src/ui/../Aircon_def.h:22:19: warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   22 | const String ON = "1";
      |                   ^~~
src/ui/../Aircon_def.h:23:7: error: unknown type name 'String'
   23 | const String OFF = "0";
      |       ^~~~~~
src/ui/../Aircon_def.h:23:20: warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   23 | const String OFF = "0";
      |                    ^~~
src/ui/../Aircon_def.h:26:3: error: unknown type name 'String'
   26 |   String zoneStatus[8];
      |   ^~~~~~
src/ui/../Aircon_def.h:27:3: error: unknown type name 'String'
   27 |   String zoneNames[8];
      |   ^~~~~~
*** [.pio/build/esp8266/src/ui/ui_events.c.o] Error 1
==================================== [FAILED] Took 2.96 seconds ====================================

Environment    Status    Duration
-------------  --------  ------------
esp8266        FAILED    00:00:02.957
=============================== 1 failed, 0 succeeded in 00:00:02.957 ===============================

Here is the inclusion


#include "ui.h"
#include "ui_helpers.h"
#include "../Aircon_def.h"

void setOnOff(lv_event_t * e)
{
	// Your code here
}

void setOperatingMode(lv_event_t * e)
{
	// Your code here
}

void setFanSpeed(lv_event_t * e)
{
	// Your code here
}

Two thoughts. (And an observation!)

  • There doesn’t appear to be a #pragma once or other header guard to prevent multiple invlusions of the header file. Not the culprit, more an observation.

  • I suspect ON/OFF are defined as int somewhere else in the 8266 code?

  • Arduino.h should have pulled in WString.h, it does for AVR boards. Maybe you should #include "WString.h" after Arduino.h.

HTH

Cheers,
Norm.

@normandunbar ,

Thanks for that.

I renamed the variable ON/OFF and add the #pragma once

The error only occurs once I include Aircon_def.h in ui_events.c.
If I remove the include the code compiles.

I tried #include “WString.h” but the error is still there.

I also tried to see if any “double” inclusion was happening and tried the “clean” them.

It is weird that the code compiles without the “include”.

The ui_events.c still doesn’t use anything from the Aircon_def.h

I have just tried just including and using WString.h (and "Arduino.h") in the ui_events.c without including Aircon_def.h

src/ui/ui_events.c: At top level:
src/ui/ui_events.c:33:22: error: unknown type name 'String'
   33 | void set_onoff_state(String state){
      |                      ^~~~~~

It doesn’t work, somehow ui_events.c cannot recognize the String?

tks

I think the type String is only defined for c++. Try renaming ui_events.c to ui_events.cpp

1 Like

Good catch. I missed that!

Cheers,
Norm.

@thebigg ,
that did the trick.
Tks.

Why is that the case?
Is there a way to change that behaviour? Should I try to change it?

@normandunbar Tks

The String class is C++, not C. When your source file has a .c extension, the gcc compiler is used. When it has a .cpp extension, the g++ compiler is used. The latter knows about classes, the former doesn’t know about them.

In PlatformIO, I almost never use .c only ever .cpp for my source files.

HTH.

Cheers,
Norm.