can you #include files win the arduino IED source files, and if so what extender

I was very glad to see the Arduino compiler allows me to build C++ classes. But once deigned, is it possible to store them in filed to be #include'ed with various projects as needed? If so, what extender should such a file have, since the source projects never seem to use *.c, *.cpp, or *.h.

You have only mentioned 3 options so I presume you have tried them all by now.

The Arduino IDE uses standard C++.

I put stuff in .h files myself - but I put code and everything in there which will probably make purists roll their eyes.

Where the Arduino IDE is inconvenient is in its inability to figure out relative paths for files. AFAIK there are three options

  • put the .h file in the same folder as the .ino file
  • put the .h file in the library folder
  • give a full path name in the #include

...R

OK, that might no be so bad then. And maybe, with some luck, the IDE will respect a few path variables I set. That might mean "inventing" a new file extension (like maybe ".ARD.H" or something) so I don't confuse other compilers. But I'll mess with it some. And yeah I occasionally put code in my .H. files too. Maybe just a little, like initializing statics which are part of a class, where C++ doesn't let you initialize it within the class definition. Or, of I want a few non OOP globals defined.

I guess my next question would be whether the compiler respects directives like ".ifdef", ".else", "ifndef", etc., I may be getting ahead of myself, but its hard to say how many projects I'll get into with Arduino, and past mistakes have taught me that waiting to organizing my development strategy until after a few MAJOR projects is a mistake. :slight_smile:

PeterPan321:
I guess my next question would be whether the compiler respects directives like ".ifdef", ".else", "ifndef", etc., I may be getting ahead of myself,

Why don't you study some of the Arduino source code to get an idea of the wide range of things that can be done?

It's a perfectly normal C++ compiler.

...R

Lots of info here.

srnet:
Lots of info here.

You know, if I had a dime for every time I saw anyone respond to anyone's question with "why don't you just ask google", I'd probably be wealthy. We all use Google all the time, but I sometimes prefer to both ask and answer questions on forums, because sometimes humans will include additional information from their own experience which was not even asked, and is usually helpful. Pointing to silly demos showing how to type words into a Google search is just insulting, so I'll take note of your user name and ignore future links posted.

Sometimes the reason people ask on a forum is to gain additional inferred insights and "gotcha's" to watch out for. Google will only return results specific to your actual words, and will not "read between the line".

PeterPan321:
but I sometimes prefer to both ask and answer questions on forums,

That's fine if you let people know that you have already Googled and you focus your question on the aspect that Google did not help with. Unfortunately I would also be rich if I got a dime for every time someone asked a question here because they were too lazy to ask Google.

...R

You're partly right about Google. Sometimes you need to know the right search terms to get meaningful links from it. However, in fact, it does "read between the lines", it incorporates world class (possibly unequalled) AI.

I haven't had any problems creating .cpp and .h files either in library folders, or in a sketch folder, and referencing them from my sketches. If you look at any library, you will see them there too.

PeterPan321:
Sometimes the reason people ask on a forum is to gain additional inferred insights and "gotcha's" to watch out for

Your question was, your words;

"I guess my next question would be whether the compiler respects directives like ".ifdef", ".else", "ifndef", etc., I may be getting ahead of myself"

To which the answer was obvious, if you had done a few seconds research, which we presume you did not.

Perhaps a better question might have been;

"I know the Arduino IDE supports ".ifdef", ".else", "ifndef", but does anyone have additional inferred insights and "gotcha's" to watch out for ?

About the Google references...

Point taken everyone. As one who has been coding and building circuit boards even beforethe days of Atari and Apple's 6502, perhaps I should know better than to bother people on a forum before exhausting every other research tool. But still, I'd just humbly ask you all to consider that asking even basic Arduino questions on an Arduino forum shouldn't immediately be considered such a sin of laziness. Consider...

  1. in a question involving the language, or even a standard library, anyone who has waded through the many variations (for example by Microsoft) in language and preprocessor constructs over the years might rightly feel feel that this forum is a safer place to look. As I've come to see that the compiler is pretty standard, I'll likely feel more comfortable starting with Google. But thats something I only discovered through questions asked here.

  2. I've already seen I'm not alone in being up around mid 60s in age on this forum, and you'd think people coding or working with electronics that long would know to hit Google first. But remember that for a lot of older hobbyists and even pros, hardware specific forums were once the ONLY place you were likely to find ACCURATE and UP TO DATE information, from FRIENDLY sources. You might even meet others with similar interests to collaborate with on a future project.

  3. It should not surprise you how many times you can pose a concise question on Google only to find links to Stack Exchange and forums like this one, where the same question was asked, and answered by snarky sounding "Go to Google" answers.

Bottom line, I'm just humbly suggesting folks lighten up a little. Granted, there are times when I see people asking for others to pretty much design and build a whole project for them, demanding an easy breezy path to completion, and sometimes its correct to do a little scolding. But maybe the "go to Google" references aren't appropriate as often as I see them. I'm a newbie on this forum, but sometimes a new commers impressions are worth noting.

is it possible to store them in filed to be #include'ed with various projects as needed?

Yes.

If so, what extender should such a file have, since the source projects never seem to use *.c, *.cpp, or *.h.

.c, .cpp, and .h will work fine.
The basic IDE build procedure:

  1. concatenate all .ino files (as text) and pre-process into a temporary .cpp file using "Arduino Algorithms" that add function prototypes and extract used libraries from the "#include <lcd.h>" and similar statements.
  2. Compile all .c, .cpp, and .S files in the user's sketch (including the .cpp file from (1)) using board-specific options plus the list of include directories from (1)
  3. Compile all of the libraries whose location was derived in (1)
  4. Compile all of the "core" files for the specific board and preferences in use.
  5. link everything together. (note that since the core and libraries were compile uniquely to each build, this involves a long list of 'fresh' ".o" and ".a" files rather than a list of "-l" linker options. I'm not sure what the state of things is WRT binary libraries.)
    See Build Process · arduino/Arduino Wiki · GitHub

That might mean "inventing" a new file extension (like maybe ".ARD.H" or something) so I don't confuse other compilers

Widespread "conventional wisdom" is to have separate directories rather than invent special filenames. All sorts of bad things have happened when "something" in an imbedded computer toolchain has managed to include "/usr/include/foo.h" instead of "/usr/local/bin/avr-toolchain/avr/include/foo.h" because of arcane inclusion rules. Otherwise, "#include" is just a text inclusion, so the files can have any name you want. Arduino internals and libraries all use ".h"

I guess my next question would be whether the compiler respects directives like ".ifdef", ".else", "ifndef", etc.,

You mean "#ifdef, #else, #ifndef", right? AFAIK, no C compiler has ".ifdef"!
Arduino uses the mainline gcc C and C++ compilers, which are "agresively standard except for all the things we do better."
As Robin mentioned in his first reply, the most likely problem you'll run into is that the compile commands end up with a LONG list of "-I" directives for include file locations, and library authors aren't all that careful about using unique filenames for their support includes. Never put your stuff in a file called "utils.h" !

westfw:
Yes.
.c, .cpp, and .h will work fine.

Thats good to know. The reason I suggested inventing my own file extender was because someone mentioned that if you put something like .H files anywhere but your project directory or the existing library directory, you'd have to specify the full path every time you #include one. That would seem to make #include directives very much a PITA to add, if for example I had to do something like...

#include "C:\Documents and Settings\Randy\My Documents\Arduino\libraries\myLEDutilities.h"

So I thought maybe by adding a PATH to the system environment vars to include the whole path (sans the actual file), might make it possible to shorten the include directives to just include the filename. But as I said when I mentioned it, I might be getting ahead of myself. It seems like the IDE has made some provisions for extending the existing library, so hopefully that will include a better means of letting it know where to find your own library files.

By the way, your explanation of how the compiler can't take advantage of a shortcut like just handling linker LNK files explains why even short programs seem to take so long to compile!

That would seem to make #include directives very much a PITA to add, if for example I had to do something like...
#include "C:\Documents and Settings\Randy\My Documents\Arduino\libraries\myLEDutilities.h"

If you want to have include files that you commonly include in Arduino sketches, you should probably go ahead and create a standard arduino library in one of the standard places. That's not much additional effort (minimally, it means just putting your directory in the right place, and it's "almost" "C:\Documents and Settings\Randy\My Documents\Arduino\libraries" anyway), and it has some additional features (keyword coloring, inclusion in Sketch/Include Library menu, shareability...)
https://www.arduino.cc/en/Guide/Libraries
There are many tutorials of varying age, findable by doing a web search for "arduino how to write a library"

just handling linker LNK files

Gee, first ".include" and now "LNK files" - what development environment are you coming from ??!
Yes, it's one reason that compiles are slower than they might be. OTOH, it means using the same source file for a dozen different chips with slightly different instruction sets and environments.
By all means turn on "verbose output" for compilation in the Arduino IDE preferences. It will show you a lot of the stuff than goes on "underneath" the IDE, that are useful to see if you're trying to understand what is going on. The IDE is a lot simpler than most people assume; it uses other tools to do most of the hard work.

PeterPan321:
if for example I had to do something like...

#include "C:\Documents and Settings\Randy\My Documents\Arduino\libraries\myLEDutilities.h"

So I thought maybe by adding a PATH to the system environment vars to include the whole path (sans the actual file), might make it possible to shorten the include directives to just include the filename.

Just put it in the right folder and it will be easy. If you put the file at:
{sketchbook folder}/libraries/myLEDutilities/myLEDutilities.h
then you can just do this:

#include <myLEDutilities.h>

You can find/set the sketchbook folder location at File > Preferences > Sketchbook location. Assuming C:\Documents and Settings\Randy\My Documents\Arduino is your sketchbook folder you're almost there already. You just need to remember that you can't dump source files directly into the libraries folder. They need to be in a subfolder. And it's better that way anyhow because putting them in subfolders helps you keep things separated and organized instead of a pile of unrelated files all jumbled together.

Well thanks folks! Yes that turned out to be way easier than I thought. I had already set up my Sketchbook location, and as advised I created a "LedUtils" folder inside the Library folder, took my whole recent "Candle" class definition and put it in a .H file (called "Candle.h" there. Sure enough, a simple "#include <Candle.h> put into a sketch allowed me to simply declare instances and write code (which now fits nicely on one page. :slight_smile: It would be nice if the IDE let me view the contents of my newly setup ".H. files in a Tab (I guess only .INO or .PDE files can be open for edit?), but Notepad++ works fine.

I've come to realize that a lot of my recent questions and pleasant surprises about this IDE are because I was initially given the impression that this Arduino IDE was a very basic and limited, and I probably took that impression too far. At first I didn't even realize it was C++ capable. Then, even when told it was a full implementation, I was waiting for the cascade of "BUTs" to drop. So far most of the things I thought would require special "guru" tricks did not. So thanks again everyone.

If you put the .h file in the root of the sketch folder then it will be displayed as a tab when you open the sketch.

If you want to use the Arduino IDE to edit libraries you need to add three files to the library folder:

  • A dummy .ino file that has the same name as the library folder. This tricks the Arduino IDE into thinking the library is a sketch. I use this file as a "to-do" list for my development work on the library.
  • A file named .development. The contents of the file don't matter. This file allows the Arduino IDE to save to the library folder, which is normally prohibited to prevent people from accidentally altering library files.
  • A file named library.properties with the format specified here: Arduino IDE 1.5: Library specification · arduino/Arduino Wiki · GitHub. You can just grab one from another library if you like. This file causes the library to be considered "Arduino 1.5 library format", which is required for the .development flag file feature.

pert:
If you want to use the Arduino IDE to edit libraries you need to add three files to the library folder:

Seems like it would be a lot easier just to use a text editor :slight_smile:

...R

pert:
If you put the .h file in the root of the sketch folder then it will be displayed as a tab when you open the sketch.

If you want to use the Arduino IDE to edit libraries you need to add three files to the library folder:

  • A dummy .ino file that has the same name as the library folder. This tricks the Arduino IDE into thinking the library is a sketch. I use this file as a "to-do" list for my development work on the library.
  • A file named .development. The contents of the file don't matter. This file allows the Arduino IDE to save to the library folder, which is normally prohibited to prevent people from accidentally altering library files.
  • A file named library.properties with the format specified here: Arduino IDE 1.5: Library specification · arduino/Arduino Wiki · GitHub. You can just grab one from another library if you like. This file causes the library to be considered "Arduino 1.5 library format", which is required for the .development flag file feature.

Thanks @pert. I'll copy and save that procedure in case I want to use the edit feature in the future. It is a lot of steps though, and as long as IDE doesn't need all that just to find .H files I #include or .CPP files I reference, I think I'll just use a different editor for now. NOTEPAD++ offers that nice syntax highlighting, which often keeps me from making dumb mistakes. I might even start using my Eclipse C++ I setup for another MCU. The Arduino IDE definitely does a lot more than I originally expected it would, but as a code editor it does leave a lot to be desired.

I really like notepad++ but I use the Arduino IDE to edit my sketches so it's nice to have a consistent UI when working on libraries too. I don't often create libraries and tend to spend a lot of time working on them so the tiny amount of time required to create 3 files is inconsequential. I would be creating the library.properties file either way and the other two can just be empty files so we're talking about maybe 30 seconds of work.

I like to use the formatting standard set by the Arduino IDE's Auto Format feature in my library code as well as my sketches for consistency. I do have notepad++ set up to do that but it's not as good because it must save the file, which the Arduino IDE does not do.