0

I have a main.ino file that contains

struct EulerAngles {
  double roll, pitch, yaw;
};

void setup() {
  EulerAngles angles;
  angles = anglesCalc();
  Serial.print(angles.yaw);
}

void loop() {
  Serial.println("In loop");
  delay(1000);                 
}

and an angles.ino file that contains

EulerAngles anglesCalc(){
   EulerAngles a;
   // calculate a
   return a;
}

When I compile the program, I get a compile error in the main.ino file:

   code/so_examples/main.ino:5:1: error: 'EulerAngles' does not name a type
 void setup() {
 ^~~~~~~~~~~
code/so_examples/main.ino: In function 'void setup()':
code/so_examples/main.ino:7:12: error: 'anglesCalc' was not declared in this scope
   angles = anglesCalc();
            ^~~~~~~~~~
code/so_examples/main.ino:7:12: note: suggested alternative: 'angles'
   angles = anglesCalc();
            ^~~~~~~~~~
            angles
[Build] Error occurred.

How do I fix this? Note that struct is declared before all else, so this answer does not seem to apply. I am using the Stino plugin for Sublime Text as the IDE.

6
  • 1
    The original question was a paraphrase of a much larger program, but in that program, I did move the struct up to the top and it did not fix the problem. I have now set up a dummy program that reproduces the problem (has the struct at the top), and have modified the question to show that real code and also the real compiler error message.
    – Obromios
    Dec 19, 2020 at 22:21
  • what IDE do you use? the main ino file must be named as the sketch folder so_examples.ino to be concatenated by the Arduino builder before the other ino files. the files are concatenated in the order of Arduino IDE tabs. arduino.github.io/arduino-cli/latest/sketch-build-process
    – Juraj
    Dec 20, 2020 at 5:41
  • 1
    Renaming main.ino to so_example.ino does not help. But I am using Sublime Text with the Stino plugin. If I open the renamed file using the Arduino IDE and compile, it works fine. So it looks like a problem with the Stino/Sublime Text IDE.
    – Obromios
    Dec 20, 2020 at 6:05
  • Arduino IDE doesn't allow to open a ino which is not in a folder with the same name
    – Juraj
    Dec 20, 2020 at 6:11
  • Good idea. The trouble with the Ardino IDE on a Mac running Big Sur is that it is unstable, it keeps crashing on me. As well, it does not seem to have any syntax checking in the editor, plus I do all my development work in Sublime Text. I will raise an issue on the Stino plugin and just stop trying to use functions which return structs until issue is rectified. Thanks to all that helped to resolve this.
    – Obromios
    Dec 20, 2020 at 6:47

3 Answers 3

1

There is an open issue with the Stino Arduino plugin for Sublime Text which means that structs are not handled properly.

Thank you to Jura and Edgar Bonet for helping me to answer this question.

2
  • the orignal question didn't mention Stino
    – Juraj
    Dec 21, 2020 at 5:49
  • I believe that the final form of the question and and accepted answer should be as clear, and if possible, not require reading comments to understand it. By editing the question to add the Stino clarification, it makes it clear it is not a redundant question. Deleting unnecessary or out of date comments also enhances the clarify of the Q and A. However, I notice you have a lot more experience in this community than me (I normally am on SO), so prepared to take your advice on this matter. I have added an acknowledgement of your and Edgar Bonet's assistance in my answer.
    – Obromios
    Dec 21, 2020 at 21:28
0

I would avoid having multiple .ino files, because of the issues raised in the other answers. If you want to split your code into multiple files, the scheme that works reliably is to have:

  • a single .ino file, named like the containing folder, holding the “main” program (this is Arduino-specific)
  • a pair of .h/.cpp files for each “module” implementing a sub-feature of the program (this is common C++ practice).

Within the .h/.cpp pair, the .h file declares everything the compiler needs for understanding code that uses the feature: type definitions (including classes) and function prototypes. The .cpp file holds the actual implementation.

Applying this scheme to your case would give:

my-sketch.ino (actually, with the name of the containing folder):

#include "angles.h"

void setup() {
  EulerAngles angles;
  angles = anglesCalc();
  Serial.print(angles.yaw);
}

void loop() {
  Serial.println("In loop");
  delay(1000);                 
}

angles.h:

struct EulerAngles {
  double roll, pitch, yaw;
};

EulerAngles anglesCalc();

angles.cpp:

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

EulerAngles anglesCalc() {
   EulerAngles a;
   // calculate a
   return a;
}

Note that including <Arduino.h> is only useful if the code uses the Arduino core API. Otherwise you can omit it. The main sketch does not need to include <Arduino.h> because it is implicitly included in the .ino file by the Arduino IDE. There is no such implicit inclusion when the IDE processes .cpp files.

1
  • 1
    it is much easier to use multiple ino files then maintain a .h files
    – Juraj
    Dec 20, 2020 at 10:38
-1

Try to add struct at the first of function, such as:

struct EulerAngles anglesCalc(){
   EulerAngles a;
   // calculate a
   return a;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.