Is it possible to split project into several folders?

It is possible to split the project into server files. However, it looks like they all must be in the same folder with the.ino file.

Is it possible to split project into several folders? I’ve got several git repositories which I want to use in the same project and I can’t use them as libraries. Each repository must have its own folder…

zhekaus:
I’ve got several git repositories which I want to use in the same project and I can’t use them as libraries.

I don't understand this. Do you have separate repositories for the different files of a single sketch? We might be able to give you better advice if you provide more information. If the repositories are published online, it would be helpful if you posted links to them.

zhekaus:
Each repository must have its own folder…

All recent versions of the Arduino IDE do recursive compilation of the src subfolder of the sketch folder. So this does allow you to split a sketch into multiple folders. However, if the root folder of the sketch is a git repository, this poses a problem for having other git repositories in subfolders. The solution would be to make the repositories in the subfolders git submodules or subtrees.

zhekaus:
It is possible to split the project into server files. However, it looks like they all must be in the same folder with the.ino file.

Is it possible to split project into several folders? I’ve got several git repositories which I want to use in the same project and I can’t use them as libraries. Each repository must have its own folder…

Do you really mean separate folders? Sounds quite messy to me and project maintenance would be most difficult.

If I have a big project, I divide it into separate tabs.

I've often split a project to different folders - by turning parts of the code into its own library, mostly to be able to reuse the code in other projects, or easily make variations of a single project. It's not that hard to do, and a great way of making your code modular and reusable in other projects.

It should be possible to have a root directory as one git repository, and other repositories in various subdirectories, by adding those in the .gitignore of the root one.

wvmarle:
I've often split a project to different folders - by turning parts of the code into its own library, mostly to be able to reuse the code in other projects, or easily make variations of a single project.
...

In this case library would be under the version control which means that two different projects may need different commit of the library.

However, we have the only copy of the library. Don’t we?

zhekaus:
two different projects may need different commit of the library.

This is a situation where Git submodules or subtrees work well (though they are kind of a pain to work with).

zhekaus:
In this case library would be under the version control which means that two different projects may need different commit of the library.

If so, you're probably doing it wrong. A library is supposed to do one specific thing, and that one thing well. #define statements can help change the behaviour of such libraries, by enabling/disabling bits. What do do with the result, that's for your main sketch to worry about.
For my hydroponics control, I have a library that controls the EC value of the solution, for example. It's passed on the pins for the fertiliser pumps and some other settings, the current and target EC values, and it will take care of the pumps. I'm using the same in different projects that need EC control.
A separate library is taking care of actually reading of the EC sensor. It is told where to find the sensor, which sensor to use even as I have two different methods, and simply returns the reading.
If I have a version of my project that just needs to read the sensor, I call just that library function. If it needs to control the value, I call the other one as well.
Likewise I have libraries for about a dozen subsystems. All taking care of their own settings (html interface - they all have a function that returns their chunk of code, and a function that picks the relevant settings out of a complete set of returned parameters).
I've gone so far that I can enable/disable complete subsystems with a single #define in a configuration file, making it really easy to enable/disable components. Disable the water level sensor? Comment out one line, recompile, and done. Components that can make use of that specific sensor will also know, though the #define, whether or not it's there and the correct code is compiled.

pert:
I don't understand this. Do you have separate repositories for the different files of a single sketch? We might be able to give you better advice if you provide more information. If the repositories are published online, it would be helpful if you posted links to them.
All recent versions of the Arduino IDE do recursive compilation of the src subfolder of the sketch folder. So this does allow you to split a sketch into multiple folders. However, if the root folder of the sketch is a git repository, this poses a problem for having other git repositories in subfolders. The solution would be to make the repositories in the subfolders git submodules or subtrees.

My git repos are local. I have different functionality split into different local repos. I use clone to use them in different projects. I don’t see any problem having many .git folders in the project folder structure. Do you? I use this technic very often.

I tried to create /src folder in the project folder. But I can’t make IDE to see the code in it, especially headers.
Indeed, I was sure that somewhen IDE could see code in subfolders.
However, currently, 1.8.10 can see the code in the root project folder only. But it’s not possible to have more than one git repo in the same folder.

wvmarle:
If so, you're probably doing it wrong. A library is supposed to do one specific thing, and that one thing well. #define statements can help change the behaviour of such libraries, by enabling/disabling bits. What do do with the result, that's for your main sketch to worry about.

Um… That why I don’t use Arduino IDE libraries for splitting the project. I have dozens of projects which are testes with particular commit of the repo. It would be madness for me to use preprocessor to adjust the library for particular case if using.

And I definitely don’t recommend to use #define as you described unless you have things simple or you develop libraries for public domain.

zhekaus:
I tried to create /src folder in the project folder. But I can’t make IDE to see the code in it, especially headers.
Indeed, I was sure that somewhen IDE could see code in subfolders.
However, currently, 1.8.10 can see the code in the root project folder only.

First of all, please use the standard terminology of the Arduino world. By "project", do you mean sketch? I know some people don't like this term, but that decision was made long ago and we're stuck with it now. A rose by any other name... Use of non-standard terms just makes it even more difficult for us to understand your question.

Arduino IDE 1.8.10 absolutely does support recursive compilation of the src subfolder of the sketch folder. I use this all the time. We would definitely have heard about it from many people by now if this was broken in any widespread manner. By your "especially headers", I suspect that you are just not forming your #include directives correctly. Let's say you have a sketch named Foo, with a component named Bar that you want to put in a separate folder. So the folder structure of the sketch looks like this:

Foo
|_Foo.ino
|_src
|_Bar
|_Bar.h

Then the #include directive for Bar.h in Foo.ino would look like this:

#include "src/Bar/Bar.h"

Two important things here:

  • You need to use the double quotes syntax in your #include directive. This causes the current folder to be searched before the libraries folders for the file.
  • Use the relative path to the file.

zhekaus:
And I definitely don’t recommend to use #define as you described unless you have things simple or you develop libraries for public domain.

For me, it is to simplify things. It was a bit more work in the original development of the library, but a lot easier to work with now. When I find an issue in the library I know it's fixed for the different sketches that rely on it.

Part of the challenge is to indeed make a library do a very specific thing, and in this case it's "read a parameter" or "handle a peripheral", where the behaviour of the peripheral is the same for all situations - it's doing a specific function, no more and no less than that function.

Any project-specific behaviour is written in the sketch of that project.

SteveMann:
Do you really mean separate folders? Sounds quite messy to me and project maintenance would be most difficult.

If I have a big project, I divide it into separate tabs.

I certainly do.
If you had a really big project (not quite big actually) you would think about splitting the project into different subprojects.
And you can’t share these ‘tabs’ you talk about with other projects. Or you can, using symbolic links for instance, but that is not my way of keeping the project in order.

pert:
First of all, please use the standard terminology of the Arduino world. By "project", do you mean sketch? I know some people don't like this term, but that decision was made long ago and we're stuck with it now. A rose by any other name... Use of non-standard terms just makes it even more difficult for us to understand your question.

Oops, sorry. I was thinking that a Project in Arduino World is a simply folder where the sketch resides. Isn’t it? Sketch is an embarrassing name for a .ino file with the same name as folder’s one. Project may consist of many files which are shown as tabs in the IDE. And these extra files are not actually the sketch – they are simply files, pieces of project’s code. Is my terminology correct?

pert:
I suspect that you are just not forming your #include directives correctly. Let's say you have a sketch named Foo, with a component named Bar that you want to put in a separate folder. So the folder structure of the sketch looks like this:

Foo
|_Foo.ino
|_src
|_Bar
|_Bar.h

Then the #include directive for Bar.h in Foo.ino would look like this:

#include "src/Bar/Bar.h"

...

Aha… Now I see...
However, that means, I have to use relative path in absolutely every subfolder!
That’s not quite convenient. Recent years I use Visual Studio for Arduino development. I simply add VS Projects into the Solution and every project can see the headers from any other project of the same solution, no matter where the project resides.

But I have the problem. I need a simple way of exporting the solution to the folder that can be opened and compiled by client with its Arduino IDE.

zhekaus:
I was thinking that a Project in Arduino World is a simply folder where the sketch resides. Isn’t it?

No, that's the sketch.

zhekaus:
Sketch is an embarrassing name for a .ino file with the same name as folder’s one. Project may consist of many files which are shown as tabs in the IDE. And these extra files are not actually the sketch – they are simply files, pieces of project’s code. Is my terminology correct?

No, the sketch is the entire folder. Quite often, sketches do only consist of a single .ino file, and for this reason it might be considered interchangeable, but you can have many files in a sketch, including .ino, .h, .cpp, .c, .s, etc. and subfolders even, so it's the whole folder. I can definitely understand that this is a strange term for someone who doesn't often work in the Arduino sphere, but it's what we have. Certainly you're not the first to call it "embarrassing". Actually this is not an invention of Arduino. It originalted with Processing, which Arduino was based on, and originally intended to be an analog of for embedded systems. To me, it's just a word that stands for a concept. Perhaps "program" or "project" would be better, but I'd rather spend my time having fun with microcontrollers than worrying about that sort of thing.

zhekaus:
However, that means, I have to use relative path in absolutely every subfolder!
That’s not quite convenient. Recent years I use Visual Studio for Arduino development. I simply add VS Projects into the Solution and every project can see the headers from any other project of the same solution, no matter where the project resides.

But I have the problem. I need a simple way of exporting the solution to the folder that can be opened and compiled by client with its Arduino IDE.

Unfortunately, I don't have another solution to offer you. Maybe now that the situation is more clear another forum member will have a great idea.

pert:
Unfortunately, I don't have another solution to offer you. Maybe now that the situation is more clear another forum member will have a great idea.

I tried your suggestion with full relative path to the header... And, you know, looks like this does not work for me. Yeah, I did make IDE see the headers. But compiler can’t find the classes’ definition now.

The log bellows shows that it doesn’t see.cpp files in the subfolder.

C:\Users\eugene\AppData\Local\Temp\ccSz9WUe.ltrans0.ltrans.o: In function `setup':

P:\pdm\Arduino\lab\IdeSubfolders/IdeSubfolders.ino:42: undefined reference to `Button::Button(unsigned char, bool, int, int, void (*)())'

C:\Users\eugene\AppData\Local\Temp\ccSz9WUe.ltrans0.ltrans.o: In function `__base_ctor ':

sketch/MyButtonListener.cpp:22: undefined reference to `EventListener::EventListener(EventEmitter*)'

C:\Users\eugene\AppData\Local\Temp\ccSz9WUe.ltrans0.ltrans.o: In function `loop':

P:\pdm\Arduino\lab\IdeSubfolders/IdeSubfolders.ino:58: undefined reference to `Button::loop()'

I am curious. How do you handle bugs in 'cloned libraries'?
I think 'cloning' libraries is a bad practice.
The equivalent of cloning the libraries with Arduino IDE would by cloning libraries - in libraries folder.

zhekaus:
you know, looks like this does not work for me. Yeah, I did make IDE see the headers. But compiler can’t find the classes’ definition now.

The log bellows shows that it doesn’t see.cpp files in the subfolder.

I'm sorry to hear that. Using Arduino IDE 1.8.10 on Windows, I just compiled a sketch that has 4 bundled libraries, all with .cpp files, and it compiles fine. So I don't know what the problem is you're having. If you can provide a sketch that has the problem for me to look at, I'd be happy to see if I can figure out what is going on.

Any update, guys? There is still no proper answer...

In brief, yeah, we can use src folder to keep the sources...
However, it looks like Arduino IDE can compile sources in the only src folder placed in sketch folder.

The question is how to have several src folders - a separate src folder for each submodule...

Juraj:
I am curious. How do you handle bugs in 'cloned libraries'?
I think 'cloning' libraries is a bad practice.
The equivalent of cloning the libraries with Arduino IDE would by cloning libraries - in libraries folder.

What’s wrong in cloning libraries from the public domain? It’s the only right way to get them and have updated.

However, I haven’t talked about cloning Arduino libraries here. Please clarify your question.

pert:
I'm sorry to hear that. Using Arduino IDE 1.8.10 on Windows, I just compiled a sketch that has 4 bundled libraries, all with .cpp files, and it compiles fine. So I don't know what the problem is you're having. If you can provide a sketch that has the problem for me to look at, I'd be happy to see if I can figure out what is going on.

Sorry, my bad, I think I did something wrong that time.

So, indeed, if I place the code to src folder, Arduino IDE does compile it.
The only problem is, in order to share this code throughout other the subfolders, I have to use full relative path everywhere. However, I think, I can bear it.

In VS, you know, things are much easier. But the problem is I have to release the project compilable with Arduino IDE state…