CPP no sense error: 'SD' was not declared in this scope

Hi all,

I'm becoming crazy with a problem that doesn't make sense.
I read a lot of thread about "'SD' was not declared in this scope" but were all mistypo error (like "#inclue" or "#include <SD.h"" etc.).

Mine isn't like that, is something that I can't resolve and maybe someone of you can help me out (probably is a logical error done by me or somewhat like that).

Think that OP as a module, that you can bring in other .ino files, instantiate and use it.
So it only need to have the include to SD.

File test.ino

// #include <SD.h> // If you uncomment this, the error *magically disappear*
#include "op.h"

void setup() {
  
}

void loop() {}

File op.h

#ifndef _OP_h
#define _OP_h

#include <SPI.h>
#include <SD.h>

class OPClass {
   public:
      void init();
};

#endif

File op.cpp

#include "op.h"

void OPClass::init() {
  SD.begin(4); // this cause the error, depending on the magic of test.ino
}

What I'm doing wrong?

You can NOT hide the use of a library. The sketch MUST include all header files that other classes will need. That is how the IDE determines what files to copy to the build directory.

If that bothers you too much, feel free to write your own IDE.

Uh oh, such a bad thing I think. I supposed it be a cpp related problem, because the error appear also if you use Arduino IDE or Visual Micro with Visual Studio.

So, in my sketch I should include ALL other headers used by cpp files? So a double inclusion?

So, in my sketch I should include ALL other headers used by cpp files? So a double inclusion?

Yes. Including a header file in the sketch makes it available in the build directory, so libraries build correctly.

If the header file is written properly, with inclusion guards, the fact that the header file is included in the sketch and in another source file is not going to be a problem.

The problem is that I'm building the test.ino on runtime when a user asks for it. This means that the test.ino could or couldn't use the module OP. This means that when I build on runtime my code, I should check for any header and bring them to main sketch.
I was thinking to use cpp exactly to have a modular programming, but with this IDE limit I can't.
Probably I will need to create for each module a separate header with only the inclusions of libraries.

Thanks anyway for your help.