2

I have a sketch with multiple source files and folders arranged like so. I've divided up the project like this to keep things clean.

|
\---project
    |   project.ino
    |   types.h
    |
    +---sub1
    |       gadget1.c
    |       gadget1.h
    |       gadget2.c
    |       gadget2.h
    |
    \---blarg
            foobar.c
            foobar.h

The sketch file includes the headers via quoted include directives (rather than the lib alternative with angled brackets). This seems to work for compilation.

However, when the IDE tries to link, all references to implementations (c source files) in the subfolders generate "undefined reference" errors. Almost as if the IDE is completely ignoring the C source files.

In trying to resolve this I've discovered that apparently the handling of subfolders by the IDE seems to be all over the place. Some versions only acknowledge headers in subfolders, others work if you put everything in a "src" folder (I've also seen mention of a "utility" folder), some people suggest modifying environment variables, etc. I've even tried manually adding source files via the "sketch/add file" option which makes the files visible in the IDE but does not solve the linking problems. All these methods seem to depend on IDE version, and I couldn't distill what was the correct way vs a hack or work-around.

So my question is what is the correct/current way of organizing the above project so that it can be compiled/linked using the IDE? I am using IDE version 1.8.5 which I believe is either the latest version, or close too it.

  • src and util are used in library folder structure, not for project folder. 1.8.5 is current release version – Juraj Jul 22 '18 at 8:09
  • I find it humorous that the other two people responding are authors of alternative IDE projects and likely don't even use the Arduino IDE. So of course they are providing erroneous information. – per1234 Jul 22 '18 at 13:08
  • sorry Per. I use Sloeber, but I am not the author :-). I knew that you will write the right answer. Majenko deleted his answer – Juraj Jul 22 '18 at 16:54
4

From Arduino IDE 1.6.10 onward the Arduino IDE compiles all source files in the src subfolder of the sketch and recursively through all folders under the src folder.

Support for compilation of sketch subfolders was added one or two IDE versions before 1.6.10 and was originally not limited to the src folder but a lot of problems were reported by people who had stored separate programs in subfolders of their sketch that caused compilation errors so this feature was restricted to the src subfolder only.

So if you structure a sketch like this:

foo
|_foo.ino
|_src
   |_bar.cpp
   |_bar.h

And use this #include directive in your sketch:

#include "src/bar.h"

it will work fine.

Almost as if the IDE is completely ignoring the C source files.

Arduino sketches are converted to C++ before being compiled. They are not C. If you want to use C in C++ code you need to wrap it in extern "C" {}

extern "C" {
#include "src/sub1/gadget1.h"
}

what is the correct/current way of organizing the above project

project
|_project.ino
|_types.h
|_src
   |_sub1
   |  |_gadget1.c
   |  |_gadget1.h
   |  |_gadget2.c
   |  |_gadget2.h
   |_blarg
      |_foobar.c
      |_foobar.h

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.