Question on tabs for Martin

Arduino projects on the go
Post Reply
bluejets
Posts: 316
Joined: 19 Jun 2019, 04:09

Question on tabs for Martin

Post by bluejets »

Hi Martin,
Feel I may have asked this previously but anyhow....... in your sketch for the gyroMPU6050 ( viewtopic.php?f=42&t=1018&hilit=gyro&start=10 ) is the only thing required to add the appropriate h and cpp to extra tabs is include them in the same program folder?
It is a good move as sometimes a certain program was written with a particular library and if not the correct one, sometime the things do not work.

Looked all over and sort of get the run-around.

Cheers Jeff
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Question on tabs for Martin

Post by Martin »

Yes, just copy the files to the folder.

The Arduino IDE works like this: the main file, for example "MySketch.ino", has to be inside a folder with the same name, "MySketch"; any files with the extensions, ".h" or ".cpp" are opened up in separate tabs when the IDE opens the .ino file.

Even though the files are opened in tabs, they won't be used by the compiler automatically: you must put a, for example, #include "MyHeader.h", in the .ino file - the compiler will then use that .h file and its matching .cpp file when it compiles the sketch. Header files, and .cpp files can also have #include statements, causing the compiler to use those files too.

You can end up with several files all #including the same .h file, so to prevent the compiler wasting time, or getting stuck in an infinite loop, it's normal to have the compiler check to see if it has already included the file from elsewhere: this is done by enclosing a code section inside a #ifndef #endif pair (short for "if token not defined") and putting a #define inside that pair that defines a unique token name. The token name could be anything, but the convention is to use the name of the header file, often capitalized, and with underscores replacing any dots.
bluejets
Posts: 316
Joined: 19 Jun 2019, 04:09

Re: Question on tabs for Martin

Post by bluejets »

Followed OK up until last paragraph........

So in place of say....... #include xyz.h ........ make it ........ #ifndef #define XYZ_H #endif
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Question on tabs for Martin

Post by Martin »

Yes, the (preprocessor) code that checks if a define has already been made, and defines a value if it hasn't, goes in each .h file.

Code: Select all

#ifndef something_h
#define something_h

(rest of the file here)

#endif
If you look at most projects (including mine) that have multiple .h files in the folder, you'll see that pattern inside most of those .h files.

It causes the preprocessor part of the C compiler to skip past code it's already 'seen' before in the current compile cycle, so the actual C code only gets compiled once. This saves time, and also prevents the compiler issuing warnings and errors about multiple redefinitions of the same variables/classes/functions that would otherwise occur.
bluejets
Posts: 316
Joined: 19 Jun 2019, 04:09

Re: Question on tabs for Martin

Post by bluejets »

Ok...thanks....I'll take a look in your projects for examples.
Thanks again...Jeff
Post Reply