Including a locally installed library in Arduino - c++

How do I include a local file? This is my project structure (with multiple sketches):
(project root)
- some_config.json
- SketchOne/
- SketchOne.ino
- SketchTwo/
- SketchTwo.ino
- lib/
- lib_1/
- some.h
From SketchOne/SketchOne.ino, I want to include lib/lib_1/some.h. I've tried the following(s):
#include "lib/lib_1/some.h"
#include <lib/lib_1/some.h>
#include "lib_1/some.h"
#include <lib_1/some.h>
Note: I use the CLI (arduino-cli)

In Arduino projects are called "sketches". The name of the main ino file of the sketch must match the name of the sketch folder. CLI builds one sketch at time.
Sketches are in file system organized into a folder called "sketchbook". The sketchbook folder should containing a special folder named "libraries". Folders in the libraries folder are treated as libraries. There are two forms of library folder in the file system: old and new.
See the sketch build process too.

Related

Accessing the same folder from different cpp files relative to the Project folder

If my project has the following folder structure:
Project
├───build
├───images
├───include
├───Apps
├───Models
├───source
└───tests
what is the best way to make the folder "images" accessable to all .cpp files inside build, tests, apps and src without using the absolute path. So every image created inside this project should be saved to the "images" folder.
I am building with Cmake if this is important(started using CMake last week so no deep knowledge). Main CmakeLists.txt file is the the root folder. Tests, Apps and source each have their own CMakeLists.txt files and executables.
Every image will be created with the same class so I think I could use std::filesystem::current_path() with a wrapper function inside the class which would generate and set the desired path but there should be another way.
I will also load files from the folder Models in the future, so the same problem.
If I am understanding your question correctly you want to access the images from the "images" folder, right? You should be thinking about the path from the point of the final executable and not the source files.
If the executable will be in the build directory then you simply need to write "../images", the 2 dots mean that you are going back 1 directory.

Gradle include all files under a directory ('recursively')

So I've been making a Java project using Gradle (GitHub), but it uses/needs JNI (natives).
I was first using Makefiles to compile and link the C++ code, but then I found out how to compile and link C++ natives using Gradle, so I got all of that working. But now I am stuck, because I can't find a way to include all natives, on the same level (base) inside of the JAR file. NOTE: I don't need help with compiling the natives, only with including/packaging them.
EDIT: I just commited the changes to GitHub.
This is my directory structure:
[Project Directory]
- build.gradle
- src/
- win32/ (the only native library that i currently have)
- cpp/
- main/
- java/
- build/
- libs/ (here is the JAR and the natives)
- win32/ (the natives)
- shared/ (the dynamic link libraries, i only want these)
- x64/ (i want to include both x64 and x86)
- mylibrary.dll (just the DLLs should be included)
- mylibrary.ext
- mylibrary.lib
- x86/
So there are a few criteria:
I only want the DLL files, none of the .ext and .lib stuff.
I want to be able to dynamically change the amount of libraries and the names of the natives.
What I have tried:
My first attempt was just looping through all folders. I didn't have to use recursion because the depth of the file structure is fixed, so it will never be further from or closer to the build/libs directory. This is how I tried coding it:
sourceSets {
main {
resources {
srcDirs "src/main/resources"
// include natives
String libfp = "${buildDir}/libs/"
File libf = new File(libfp);
if (!libf.exists())
libf.mkdir();
FileFilter isDir = f -> f.isDirectory();
FileFilter isDll = f -> f.getAbsolutePath().endsWith(".dll");
for (File file : libf.listFiles(isDir)) { // iterate "libs" to find all libraries
// enter "shared"
File filen = new File(file.getAbsolutePath() + "/shared/");
for (File file1 : filen.listFiles(isDir)) { // iterate over "shared" to find all platforms
for (File file2 : file1.listFiles(isDll)) { // get all dlls
include(file2.getAbsolutePath())
}
}
}
}
}
}
This worked, except from the including itself. I don't know if I understand how this works correctly, but the include function didn't seem to add anything to the resources.
Then, I checked the documentation and found it was a pattern based function, so I tried making a simple include call with the pattern I thought would work:
include "/build/libs/**/*.dll"
// I also tried the following:
include "/build/libs/**.dll"
include "/build/libs/*.dll"
But that didn't seem to work too. So I think I am just misunderstanding how the include function works.
Just use
include '/build/libs/**'
will work. Thanks.

Eclipse C++ including header file from my source folder

I'm pretty new to C++ and Eclipse in general so I apologise if I'm missing something fairly obvious.
The problem I'm having is that I'm trying to include a header file in one of my source files but they're in different folders in my project directory. I have no idea how I should be including them. I've uploaded an image showing my problem with the header file I want to include highlighted.
If someone could tell me what '#include' statement I should be using them that would be brilliant.
Thanks!
There are a couple of different options to make this work. Simplest is to change the #include to
#include "../Statistics/Statistics.h"
This will work without any other modifications. However, if you move either file, or somehow change the relative path between the two, this will break.
Alternately, you can add the path to the Statistics folder to your compiler's include file search path. Right click on the project name, select Properties -> C/C++ Build -> Settings and then find the includes files path option for your compiler. For g++, it is -I<path/to/include/folder>. Adding this will make the #include statement work as you currently have it.
A very similar option to the second one is to add the path to the src folder (instead of the Statistics folder) to the includes search path. In this case, you'll have to change the statement to
#include "Statistics/Statistics.h"
When you create subfolders in your src folder then each cpp file is compiled in that folder it is located in. Thus, any "" includes need to specify the relative path to get from that folder to another.
In your case, to get from inside the FileInOut folder you need to go back one level and then into the Statistics folder
eg
#include "../Statistics/Statistics.h"
Another alternative is, if you are keeping your includes in your src directory, to add the src directory to the include path. Now when you include you need only specify the path from the src root.
eg.
#include "Statistics/Statistics.h"

C++ Project: Artifact-Name dependant "include" directory population using CMake (build-time)

I've devised a good structure for a new project which I currently successfully use.
NOTE: This structure is beneficial for the project which is developed by multiple programmers.
<Project Directory>
+ include
+ Haroogan
+ Utility
- util.h
+ More
- more.h
+ Whatever
- whatever.h
+ src
+ Haroogan.More <--- Artifact
- more.h
- more.cpp
- CMakeLists.txt
+ Haroogan.More.Whatever <--- Artifact
- whatever.h
- whatever.cpp
- CMakeLists.txt
+ Haroogan.Utility <--- Artifact
- util.h
- util.cpp
- CMakeLists.txt
+ Haroogan.Application <--- Artifact
- app.h
- app.cpp
- CMakeLists.txt
- CMakeLists.txt <--- "Root" CMakeLists.txt
Artifact - is library, executable, etc. - you got the point.
Artifact-Name (as in the subject) - is simply the name of the artifact which is deduced by CMake from the name of the directory dedicated to the artifact. Actually terms Artifact and Artifact-Name are essentially the same. For example: artifact residing in directory "Haroogan.More.Whatever" has name "Haroogan.More.Whatever".
This has several consequences:
library, executable, etc. produced after a build will be named with Artifact-Name;
all source code pertaining to a particular artifact is enclosed in a namespace corresponding to the Artifact-Name. For instance: artifact "Haroogan.More.Whatever" imposes "Haroogan::More::Whatever" namespace on all its sources;
when one artifact wants to use another one, then one has to include another one's headers and optionally link against it. However, we all know writing #include "../Haroogan.More/more.h" not only looks messy, but also breaks the basic idea that artifacts actually represent stand-alone components which have to be decoupled even in terms of file-system. Moreover, the concept of private headers is also broken, because this way I can access any headers inside other artifacts.
What we need here is simply a public header repository - the "include" directory. Therefore, to address the last issue - I decided to do the following:
each artifact decides (in its CMakeLists.txt of course) on its own which headers it wants to export to the outside world;
then it copies (on every build, but only when needed of course) these header files to the corresponding directory inside "include". For instance, if Artifact-Name is "Haroogan.More.Whatever" then headers will be copied to "include/Haroogan/More/Whatever/" directory (as shown above).
I believe it's a nice and robust approach since now if I want to use "whatever" and "more" classes from "Haroogan.More.Whatever" and "Haroogan.More" artifacts in other components - I simply write:
#include <Haroogan/More/Whatever/whatever.h>
#include <Haroogan/More/more.h>
using Haroogan::More::Whatever::whatever;
using Haroogan::More::more;
The system works like a charm (I can provide CMake scripts if anybody wants). However, I'm not satisfied with the fact that headers are copied. It would be much better if, for example, instead of copying "whatever.h" CMake would create new file "whatever.h" in "Haroogan/More/Whatever/" and inject #include "../../../../src/Haroogan.More.Whatever/whatever.h" in it.
My system right now it fully automated. In other words, path "Haroogan/More/Whatever" is automatically deduced from Artifact-Name "Haroogan.More.Whatever". Therefore, it would be great if injection of #include "../../../../src/Haroogan.More.Whatever/whatever.h" with all those nasty ../../ would be also automated.
Unfortunately, I'm new to CMake and don't know how to achieve this functionality, but I think it is possible and might be already done by someone. Thank you.
EDIT:
A temporary solution for this problem could be the following:
Instead of creating "whatever.h" inside "Haroogan/More/Whatever/" which immediately leads to dealing with ../../ mess, I could simply create "Haroogan.More.Whatever.whatever.h" (by prefixing "whatever.h" with "Haroogan.More.Whatever") right in the "include" directory and use it as:
#include <Haroogan.More.Whatever.whatever.h>
using Haroogan::More::Whatever::whatever;
This solution is acceptable, but I don't like it as much as the one I'm interested in.
How about this:
macro(add_public_headers)
foreach(header ${ARGN})
get_filename_component(abspath ${header} ABSOLUTE)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${header} "#include ${abspath}"
endforeach()
endmacro()
This maro can be used now in such way:
In src/Haroogan.More.Whatever/CMakeLists.txt you do
add_public_headers(whatever.h)
and this will generate header with single #include line in your build dir.
The only thing is that path would be absolute, but it shouldn't be problem for you.

Keeping all libraries in the Arduino sketch directory

I know that you are supposed to place any external libraries under the "libraries" folder of the arduino install directory, but I have a project that uses several libraries that I have created for the project and mainly to keep all that code self contained and out of the main pde file. However, I have tried to place the libraries in the same directory as the main PDE file so that I can more easily keep everything synced up in subversion (I work on this on multiple computers) and I don't want to have to keep going back and syncing up the libraries separately. Also, just for the sake of being able to easily zip of the sketch folder and know that it contains everything it needs.
I've tried adding the header files to the sketch as a new tab, but that doesn't seem to work at all... don't even care if they should up in the arduino IDE.
I've also tried adding the libraries to the sketch directory in subdirectories (what I would greatly prefer) and then linking to them as:
#include "mylib/mylib.h"
and
#include <mylib/mylib.h>
But both of these result in file not found errors.
Is this possible? And, if so, how do I include them in the main file for building? Preferably in their own subdirectories.
I had the same issue. Solved it for Arduino IDE > 1.8. Seems a specialty in newer IDEs (?) according to the reference (see bottom link).
You have to add a "src" Subdirectory before creating a library folder. So essentially your project should look like this:
/SketchDir (with *.ino file)
/SketchDir/src
/SketchDir/src/yourLib (with .h and .cpp file)
and finally in your sketch you reference:
#include "src/yourLib/yourLib.h"
otherwise in my case - if I am missing the "src" folder - I get the error message that it cannot find the yourLib.cpp file.
Note: I am using a windows system in case it differs and actually VS Code as wrapper for Arduino IDE. But both IDE compile it with this structure.
References:
https://forum.arduino.cc/index.php?topic=445230.0
For the sketches I have, the "*.h" and "*.cpp" library files actually reside in the same folder as the sketch, and I call them like "someheader.h". I also noticed that if I go into sketch menu and add file... that the file doesn't appear until I close and reopen the sketch.
I agree with you; this is an intolerable way to develop software: it requires every file that you need to be in the same directory as the main program!
To get around this, I use make to put together a single .h file from my .h and .cpp sources - you can see this used in this Makefile:
PREPROCESS=gcc -E -C -x c -iquote ./src
# -E : Stop after preprocessing.
# -C : Don't discard comments.
# -x c : Treat the file as C code.
# -iquote ./src : Use ./src for the non-system include path.
TARGETS=sketches/morse/morse.h
all: $(TARGETS)
clean:
rm $(TARGETS)
%.h: %.h.in
$(PREPROCESS) $< -o $#
Arduino is very picky about file endings - if you put a .cpp or .cc file in its directory it automatically uses it in the source, and you can't include anything that's not a .cpp, .cc or .h - so this is about the only way to do it.
I use a similar trick also to put together JavaScript files here.
This requires that you run make after editing your files, but since I'm using an external editor (Emacs) anyway, this is zero hassle for me.
Unfortunately the Arduino IDE is awful and shows no signs of improving. There is no real build system so it only lets you build programs that reside in a single directory.
The only real solution is to write a makefile, then you can use a real IDE. I'm hopeful that one day someone will write an Arduino plugin for QtCreator.
Here's an example makefile:
http://volker.top.geek.nz/arduino/Makefile-Arduino-v1.8
I just had this same problem (I also like to keep the code self-contained), so I'll just jot down some notes; say I have a MyPdeSketch.pde using MyLibClass.cpp; then I have it organized like this
/path/to/skdir/MyPdeSketch/MyPdeSketch.pde
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.h
(In principle, /path/to/skdir/ here is equivalent to ~/sketchbook/)
What worked for me is something like:
mkdir /path/to/arduino-0022/libraries/MyLibClass
ln -s /path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.* /path/to/arduino-0022/libraries/MyLibClass/
After restart of the IDE, MyLibClass should show under ''Sketch/Import Library''.
Note that the only way I can see so far for a library class file to refer to other library files is to include them relatively (from 'current location'), assuming they are all in the same main arduino-0022/libraries folder (possibly related Stack Overflow question: Is it possible to include a library from another library using the Arduino IDE?).
Otherwise, it should also be possible to symlink the MyLibClass directory directly into arduino-0022/libraries (instead of manually making a directory, and then symlinking the files). For the same reason, symlinking to the alternate location ~/sketchbook/libraries could also be problematic.
Finally, a possibly better organization could be:
/path/to/skdir/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyLibClass/MyLibClass.h
/path/to/skdir/MyLibClass/MyPdeSketch/MyPdeSketch.pde
... which, after symlinking to libraries, would force MyPdeSketch to show under the examples for the MyLibClass library in Arduino IDE (however, it may not be applicable if you want to self-contain multiple class folders under a single directory).
EDIT: or just use a Makefile - which would work directly with avr-gcc, bypassing the Arduino IDE (in which case, the sketchbook file organization can be somewhat loosened)..
Think I know what do u need exactly.
you have a project folder say MYPROJ_FOLDER and you want to include a Libraries folder that contains more children folders for your custom libraries.
you need to do the following:
1- create folders as follows:
-MyProjFolder
-MyProjFolder/MyProjFolder
and then create a file with the folder name in .ino extension
-MyProjFolder/MyProjFolder/MyProjFolder.ino
2- create libraries folder:
-MyProjFolder/libraries <<<<< name is not an option should be called like that.
3- then create your own libraries
-MyProjFolder/libraries/lib1
-MyProjFolder/libraries/lib1/lib1.cpp
-MyProjFolder/libraries/lib1/examples <<<< this is a folder
-MyProjFolder/libraries/lib1/examples/example1
repeat step 3 as much as you want
also check http://arduino.cc/en/Guide/Libraries
I did it a little differently. Here is my setup.
Visually this is the directory layout
~/Arduino/Testy_app/ <- sketch dir
/Testy_app.ino <- has a #include "foo.h"
/foo <- a git repo
/foo/foo.h
/foo/foo.cpp
Here is how I build:
~/Arduino/Testy_App/$ arduino-cli compile --library "/home/davis/Arduino/Testy_app/foo/" --fqbn arduino:samd:mkrwan1310 Testy_app
If you wish to be more elaborate and specify libs and src dirs, this also works
~/Arduino/Testy_app/ <- sketch dir
/Testy_app.ino <- has a #include "foo.h"
/lib <- a git repo
/lib/foo/src/foo.h
/lib/foo/src/foo.cpp
and the build method is:
~/Arduino/Testy_App/$ arduino-cli compile --library "/home/davis/Arduino/Testy_app/lib/foo/src" --fqbn arduino:samd:mkrwan1310 Testy_app
One more bit of tweaking needs to be done to include files from the lib dirs to main dir. If you need to do that, this is the work around:
~/Arduino/Testy_app/ <- sketch dir
/Testy_app.ino <- has a #include
"foo.h"
/inc/Testy_app.h
/foo <- a git repo
/foo/foo.h
/foo/foo.cpp < has a "include testy_app.h"
Then do the compile like this
~/Arduino/Testy_App/$ arduino-cli compile \
--library "/home/davis/Arduino/Testy_app/inc" \
--library "/home/davis/Arduino/Testy_app/foo/src" \
--fqbn arduino:samd:mkrwan1310 Testy_app
What has worked for me is to create a dir, for example "src" under the sketch dir, and under that a dir for each personal library.
Example:
I have a project called ObstacleRobot, under that a folder for my sketch, named obstaclerobot (automatically created by the IDE) and there my sketch "obstacleRobot.ino"
Up to now we have:
/ObstacleRobot
/obstaclerobot
obstacleRobot.ino
Then I wanted to include a personal library that was fully related with this project and made no sense in including it in the IDE libraries, well in fact I want to do this for each part of the robot but I'm still working on it.
What in the end worked for me was:
/ObstacleRobot
/obstaclerobot
obstacleRobot.ino
/src
/Sonar
Sonar.h
Sonar.cpp
Then what you have to do in the main sketch is to write the include as follows:
#include "src/Sonar/Sonar.h"
And thats all.
Following the lines of Hefny, make your project an example for your library.
For example (Unix env), let's say the libraries are in ~arduino/libraries
Your create your project ~arduino/libraries/MyProject, your libraries go there (for example ~/arduino/libraries/MyProject/module1.h ~/arduino/libraries/MyProject/module1.cpp ~/arduino/libraries/MyProject/module2.h ~/arduino/libraries/MyProject/module2.cpp
Now:
mkdir -p ~arduino/libraries/MyProject/examples/myproj
edit ~arduino/libraries/MyProject/examples/myproj/myproj.ino
(note that this is not examples/myproj.ino but examples/myproj/myproj.ino)
Restart the IDE, you should find your project in the menu File/Example/MyProject.
Also note that you do the include with #include
Why dont we just write a script with a single copy command, copying our libs from wherever our library is located into the arduino IDE library folder?
This way we keep the file structure we want and use the IDE library requirements without fuss.
Something like this works for me:
cp -r mylibs/* ~/Documents/programs/arduino-1.5.8/libraries/.
Note that the paths are relative to my own file structure.
Hope this helps someone. This includes my future self that I bet will be reading this in a near future... as usual!
J