How to avoid #include on new sketch

Hello all.

I have some function with headers (a.h and b.h for exemple) located on the arduino default library folder.

So when I create a new sketch, I add to include those header on the top of the sketch
**#include <a.h> and #include <b.h>** to be able to use them.

To avoid #including all personnal header each time we create a new sketch, is there a way to modify arduino files so files are included automatically ?

For example add the headers in the arduino main.cpp file ?

I'd make a "master.h" file that #include(s) all the others. Then you just need

#include <master.h>

thanks @gfvalvo, it's a good idea, but my goal (if possible) is to make this transparent, so no need to #include at each new sketch

Change your minimum basic file to have the includes already there: Set the default file/new sketch

1 Like

That's not transparency, that is hiding details.

If I was reading your code, soon enough I'd see things to wonder about. The first look back would be up top to see what you included… hmmm, nothing included, where is DOG_WEIGHT defined?

a7

2 Likes

Thanks ! A very good idea, mixed with @gfvalvo idea.
I found the step for IDE > 2 here how to setup default new sketch folder
(I've just discovered that the IDE has is own command palette ^^)

But #include still appears on new sketch.
So, I maintain my first question, if someone have an idea ^^
I wish I can't see any #include on the top of new sketches

Why would you possibly want that??? As @alto777 pointed out, it's a recipe for confusion and painful debugging.

2 Likes

To do this, you would need to add to your sketch the many thousands of lines that each library file "includes."

In the same way, you don't care about seeing #include "digital.h" or #include "analog.h", I want that, if I always use a custom function, it's already automatically included into my sketches.

You would add #include directives for the header files to the Arduino.h file in the cores of the boards you are working with. The Arduino sketch preprocessor automatically adds an #include directive for Arduino.h to the top of the sketch before compiling.

If you enable verbose compilation output in the Arduino IDE preferences and then compile a sketch, you will see the base location of the core in the black Output panel at the bottom of the Arduino IDE window. For example, if the output looks like this:

Using core 'arduino' from platform in folder: C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Then the path of the file is:

C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino\Arduino.h

:exclamation: If looking for it with your file manager or command line, note that the AppData folder is hidden by default. On Windows "File Explorer", you can make it visible by opening the "View" menu, then checking the box next to "☐ Hidden items".

But I agree with others that it is a bad idea. You would have to redo this after every time you update a boards platform and it means your sketches will only compile in your modified environment so you can't easily share them with others.

1 Like

thank you @ptillisch that's what I need !
I tried what you said, but I got compilation error, maybe my function is not located at the right place

First, I put my function (monitor.h) in default Arduino Library folder :
C:\Users\PC\Documents\Arduino\libraries\my_function\src\monitor.h

2nd : I add #include "monitor.h" into the Arduino.h right core file
(I use minima, so my Arduino.h path is :

C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino

3d, when I compile, I get this error

What am I doing wrong ? My function location ?

same problem

FQBN: arduino:renesas_uno:minima
Using board 'minima' from platform in folder: C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5
Using core 'arduino' from platform in folder: C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5

loading library from c:\Users\PC\Documents\Arduino\libraries\monitor: invalid library: no header files found
Detecting libraries used...
C:\Users\PC\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\7-2017q4/bin/arm-none-eabi-g++ -c -w -Os -g3 -fno-use-cxa-atexit -fno-rtti -fno-exceptions -nostdlib -DF_CPU=48000000 -DARDUINO_UNOR4_MINIMA -std=gnu++17 -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsigned-char -ffunction-sections -fdata-sections -fmessage-length=0 -fno-builtin -w -x c++ -E -CC -DARDUINO=10607 -DPROJECT_NAME="C:\Users\PC\AppData\Local\Temp\arduino\sketches\A26535C3607749BBF0297681D21FC4CD/sketch_jan11a.ino" -DARDUINO_MINIMA -DARDUINO_ARCH_RENESAS_UNO -DARDUINO_ARCH_RENESAS -DARDUINO_FSP -D_XOPEN_SOURCE=700 -mthumb @C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\variants\MINIMA/defines.txt -DCFG_TUSB_MCU=OPT_MCU_RAXXX -IC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino/tinyusb -IC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino/api/deprecated -IC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino/api/deprecated-avr-comp -IC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino -IC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\variants\MINIMA -iprefixC:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5 @C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\variants\MINIMA/includes.txt C:\Users\PC\AppData\Local\Temp\arduino\sketches\A26535C3607749BBF0297681D21FC4CD\sketch\sketch_jan11a.ino.cpp -o nul
Alternatives for monitor.h: []
ResolveLibrary(monitor.h)
In file included from C:\Users\PC\AppData\Local\Temp\arduino\sketches\A26535C3607749BBF0297681D21FC4CD\sketch\sketch_jan11a.ino.cpp:1:0:
C:\Users\PC\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.5\cores\arduino/Arduino.h:8:10: fatal error: monitor.h: No such file or directory
 #include "monitor.h"
          ^~~~~~~~~~~
compilation terminated.
  -> candidates: []

exit status 1

Compilation error: exit status 1

PS: I do understand all advice for this bad idea, but Its useful for me to know it

Thanks for the warning, I won't try debugging any sketches like that!

There is a big misunderstanding here... what is the problem if don't want to see my #include function on top of my sketches ?
It's a bit weird to think bad about a simple question which is about how I want to organize my sketches... very weird reaction here...
I don't have the right to organize my sketches like I want ? strange...

I do understand that It can make bug so, thanks to your responses, I have the second option if it's the case.

So I'll appreciate if someone can help me reach the initial question, without jugging

Programming has conversations. These exist because in real world programming, you are not likely to be the only person on the only computer working on your program, and if you are, you are not likely to be the person to maintain it. Straying from these conventions will always draw judgement, and if you plan on willfully straying, it's something that you'll need to get used to.

You can all but write off future help debugging programs here, if you have hidden includes.

Good luck

1 Like

"Tell me what I want to hear, and don't tell me why it's a bad idea."

Wow.

please, @van_der_decken and @er_name_not_found
if you don't want to help, please don't waste you time, please let others openminded answer if they want

I say this with all respect: everyone here has been trying to help you.

You just don't want to listen to what they have to say if it conflicts with your preconceived notions.

1 Like

You asked for help asking to making a double male extension cord.

I already helped and my last message was meant to help too. You will face issues with this.

So, to go in the right way, let me explain one exemple :

I usually need tu Serial.print variables to debug, so I make a function to be able to use for exemple monitor("var1name",var1name, "var2name",var2name,...) with some code inside to make tabulation and other stuff I need for a good printing.

I got many functions like that and each time I create a sketch, I have to #include all, and with all global variables and others stuff, I don't like seeing a lot of coding before the setup(), that's why I'm often looking for a way to make lighter my coding.
So it will be helpull for me If the #include of a specific folder (where I put my often used function) is automatic.
I don't want to modify minima core folder, just if possible adding a #include of my folder somewhere, so Even If I update my ide, I just have to re-write this #include and thats all.

Again, I know now thanks to @er_name_not_found and @gfvalvo that there is one way to have minimum #include, but I'll be more satisfy if I find a way to my first question.