Arduino-Makefile icon indicating copy to clipboard operation
Arduino-Makefile copied to clipboard

Library autodetection doesn't work when one library uses another library

Open matthijskooijman opened this issue 11 years ago • 25 comments

I'm using the ShiftOutX library, which uses the SPI library. Arduino-Makefile fails to detect this case, resulting in a SPI.h not found error message.

matthijskooijman avatar Jun 27 '13 15:06 matthijskooijman

Right now the makefile only auto detects the included libraries from the local source files. This was introduced in commit eef73003c939e418a0cb09ca1dcaac9b86c2c5d9

I am not sure if we can easily extend this auto detection to libraries as well. Do you have any suggestion for it?

Also as part of issue #45, when we add support for Arduino 1.5.x, we will be implementing Arduino 1.5.x library specification. In this specification the meta file will have the dependency list for libraries.

Till then I guess you might have to manually set ARDUINO_LIBS

sudar avatar Jun 28 '13 03:06 sudar

Why not just #include <SPI.h> in your main sketch. That would be the easy fix... or does that fail too?

xxxajk avatar Jul 12 '13 23:07 xxxajk

Haven't actually tried that, but I expect it to work. Still, it would be better if it would just work right away, but it's not any kind of priority...

matthijskooijman avatar Jul 13 '13 08:07 matthijskooijman

Arduino IDE does not do this either! IIRC you need to place every include you use in the sketch for everything to be located.

xxxajk avatar Nov 29 '13 20:11 xxxajk

Marking it as wontfix, since Arduino IDE doesn't support it.

But I would be happy to merge it if someone can implement it.

sudar avatar Dec 01 '13 07:12 sudar

shouldn't this be closed as its a wontfix ?

sej7278 avatar Feb 01 '14 01:02 sej7278

I'd love to work on that! :)

Having to declare in your main sketch all the libs that your libs use is kind of cumbersome and prone to errors...

@sudar @sej7278 where should I start?

ladislas avatar May 20 '14 07:05 ladislas

@tinyladi,

Nice to know that you are interested in working on this :)

One word of caution though. Since this is something that is not supported by Arduino IDE, it would be nice to have this feature turned off by default and enabled only if a flag is set in the makefile. We would like to maintain compatibility with Arduino IDE as much as possible.

Having said that, you could look at this https://github.com/sudar/Arduino-Makefile/commit/c3dc56bd3a33d714cc0366f82fa300d48e3638f7 I tried doing something similar but left it mid-way.

sudar avatar May 20 '14 09:05 sudar

For the record, see also https://github.com/arduino/Arduino/issues/236 There has been some work to support this in the 1.5.x IDE, I expect this will be finished in the near future.

matthijskooijman avatar May 20 '14 09:05 matthijskooijman

@sudar I'm doing some tests, is it possible to create a test branch in your repo I could PR to so that people could test as well? :)

@matthijskooijman can you tell me how you organize your sources? if you have a github repo, it would be even better.

ladislas avatar Aug 14 '14 15:08 ladislas

@ladislas, what are you interested in specifically? I mean I have a sketches folder, containing a libraries folder, containing libraries, but that's pretty standard (and the IDE doesn't allow for a lot of other layouts?)

matthijskooijman avatar Aug 14 '14 15:08 matthijskooijman

@matthijskooijman well you can organize your code as you like if you use the Makefile, for example...

But all your libraries are at the same place? something like:

Lib-folder/
    FirstLib/
    SecondLib/
    ThirdLib/

ladislas avatar Aug 14 '14 15:08 ladislas

I've actually stopped using this Makefile when I switched to 1.5.x, but I stuck to the classic layout before that as well (always good if you can fall back to the IDE).

Anyway, I have them like you suggest, yes.

matthijskooijman avatar Aug 14 '14 15:08 matthijskooijman

@ladislas I have created a new branch called auto-lib to which you can send the pull request.

While you are at it also checkout https://github.com/sudar/Arduino-Makefile/commit/c3dc56bd3a33d714cc0366f82fa300d48e3638f7 which I did sometime back.

Thanks for looking into it.

sudar avatar Aug 14 '14 15:08 sudar

Thank you very much @sudar :) I'll push in tonight or tomorrow.

Right now, auto-detection works. The work around is to use:

ARDUINO_LIBS +=  $(notdir $(wildcard $(USER_LIB_PATH)/*))
# instead of
ARDUINO_LIBS += $(filter $(notdir $(wildcard $(USER_LIB_PATH)/*)), \
        $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(LOCAL_SRCS)))

The problem is that it lists all the libraries, even if they're not used in the current sketch being compiled. But that's just the beginning :)

ladislas avatar Aug 14 '14 16:08 ladislas

i was thinking about this just the other day, and as the IDE and the makefile compile all of the core libs even if they're not used, would it really harm to do the same for user libs - i.e. compile everything and leave it to the linker to not link unused ones? so @ladislas one-liner above is enough of a fix....?

sej7278 avatar Aug 14 '14 21:08 sej7278

@sej7278 well yes and no.

Yes because it seems to be working fine, on my project at least, and if you say that the IDE does the same, it sounds good.

No because the show_config_info will list all the libs available and not only the libs used with the sketch. If it's not an issue, that's alright.

But I'm trying to find a more elegant solution.

ladislas avatar Aug 14 '14 21:08 ladislas

yeah i agree its not elegant (and a bit slow the first time) and the IDE doesn't quite do that - it does for the core but not the user libs.

i was thinking of a recursive grep rather than a bunch of sed, but it was a random thought at 3am ;-)

sej7278 avatar Aug 14 '14 22:08 sej7278

That won't work - if two libraries define functions by the same name, or the same ISR, the linker will barf when they both are linked at the same time (even if not needed, the linker doesn't look at #include lines).

matthijskooijman avatar Aug 14 '14 22:08 matthijskooijman

Hello, is this issue still unsolved?

pglu avatar Feb 22 '22 17:02 pglu

I'm happy to see someone else has forked the project! That said... add this to the top of your .ino, and it will work IDE or not: #include <Arduino.h>

After you do that, it will detect everything, At least it does in my trunk, which this fork is based on...

xxxajk avatar Feb 23 '22 06:02 xxxajk

Hi, thanks for your quick reply, I am also happy to be able to use your project. However I'm still struggling with some libraries, but will check out including Arduino.h. The reason may be, that on my Raspi IDE 1.0.5 is instalIed don't know how to update it.

pglu avatar Mar 02 '22 18:03 pglu

No need to include Arduino.h, read the source, that's already done.

And this isn't a fork of anything from xxxajk, don't feed the troll

sej7278 avatar Mar 02 '22 21:03 sej7278

While this isn't a fork of my effort, both have a common root, which IIRC is pre-github days.

xxxajk avatar Mar 03 '22 17:03 xxxajk

Yup, first version of this we have history of this was 2010

sej7278 avatar Mar 04 '22 13:03 sej7278