As a new user to IDE v2, I have wasted a huge amount of time searching the forum for a clear explanation of where to place a simple self-generated include file for inclusion in an ino file. Why is it so difficult for such a basic process to be explained simply and clearly?
Hi @nobbyh. You can put it in the same folder as the .ino
file if you like.
You must use the double quotes syntax in the #include
directive for header files that are in the sketch folder.
For example, if your sketch has this structure:
SomeSketch/
├── SomeHeader.h
└── SomeSketch.ino
Then your #include
directive in SomeSketch.ino
should be like this:
#include "SomeHeader.h"
I moved your topic to an appropriate forum category @nobbyh.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Hi ptillisch, Thanks for the prompt reply. Strange to say your suggestion was the exact thing I tried just before I posted; it made no difference to my error messages.
I'm going to ask you to post the full output from a compilation.
This procedure is not intended to solve the problem. The purpose is to gather more information.
Please do this:
- Select Sketch > Verify/Compile from the Arduino IDE menus.
- Wait for the compilation to fail.
- You will see a "Compilation error: ..." notification at the bottom right corner of the Arduino IDE window. Click the "COPY ERROR MESSAGES" button on that notification.
- Open a forum reply here by clicking the "Reply" button.
- Click the
<CODE/>
icon on the post composer toolbar.
This will add the forum's code block markup (```
) to your reply to make sure the error messages are correctly formatted.
- Press Ctrl+V.
This will paste the compilation output into the code block. - Move the cursor outside of the code tags before you add any additional text to your reply.
- Click the "Reply" button to post the output.
In case the output is longer than the forum software will allow to be added to a post, you can instead save it to a .txt
file and then attach that file to a reply here:
- Open any text editor program.
- Paste the copied output into the text editor.
- Save the file in
.txt
format. - Open a forum reply here by clicking the "Reply" button.
- Click the "Upload" icon () on the post composer toolbar:
A dialog will open. - In the dialog, select the
.txt
file you saved. - Click the "Open" button.
- Click the "Reply" button to publish the post.
Alternatively, instead of using the "Upload" icon on the post composer toolbar as described in steps (5) - (7) above, you can simply drag and drop the .txt
file onto the post composer field to attach it.
Thanks again, will comply when I have tidied up some other error messages
HI ptillisch again. Thank you for you help; my previous attempt to switch from < > to " ", obviously hadnt registered properly but now works as you suggest. So now we can say clearly, if one wishes to break up a long main program (ino or cpp) into smaller parts, then one can just store the sub code as a series of xxx.h files alongside the main program, and then add #include "xxx.h" statements in the program at appropriate locations. However the question still arises " what about the #include <xxx.h> case, where is there library located?.
I still have an error problem, but it is of an entirely different nature! It is complaining of "/357" and "/273" codes in the included h file. This obviously needs to be really chased up under an entirely different topic; back to the salt mines!
The /357 etc problems solved by the copy and re-paste method. I guess i should consider this topic now closed.
You can also use multiple .ino files, without needing #include directives.
The IDE will concatenate these into a single C++ file during pre-processing.
You are welcome. I'm glad it is working now.
The globally installed libraries are installed under the libraries
subfolder of your sketchbook folder.
You can find the location of the sketchbook folder by selecting File > Preferences from the Arduino IDE menus and then noting the path shown in the "Sketchbook location" preference.
Although we normally install libraries to that location indirectly using the Arduino IDE Library Manager, you are also welcome to manually add libraries to that folder.
The header files in the libraries installed at that location are available to reference in your sketch using the angle brackets #include
directive syntax. Even though the angle brackets syntax is most correct in this case, you can also use the double quotes #include
directive syntax with those header files from the installed libraries as well as for the file in the sketch folder so don't get confused when you see sketches written by less detail oriented developers using the quote syntax inappropriately for header files from libraries.
The typical Arduino library has a somewhat complex structure, with source files, a special metadata file, and example sketches. However, an Arduino library can be as simple as a single .h
file in a folder.
For example, if you create a library with this structure:
<sketchbook location>
├── libraries/
│ ├── SomeLib/
│ │ └── SomeLib.h
│ ...
...
(where <sketchbook location>
is the folder at the path from your "Sketchbook location" preference)
You can use this #include
directive in your sketch:
#include <SomeLib.h>
No, that's a recipe for creating lots of "Multiple Definition" errors from the linker. See My Post #5 in this Thread for a simple guide to properly breaking up large programs for better modularity.
Thank you ptillisch for that very thorough explanation.
Thank you gfvalvo for the reference which is very helpful
You are welcome. I'm glad if I was able to be of assistance.
Regards,
Per
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.