-
-
Notifications
You must be signed in to change notification settings - Fork 114
#include or #error inside #if not working as expected #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Inside libraries, no preprocessing is done, so you will always need to include what you need. Including Arduino.h is easiest, since that will get you mostly everything. This has been the case as long as I remember. However, inside the sketch it should just work. I just tried your sketch, and got the same error. Something weird is going on here. Look at the command used to compile the sketch:
Note that this does not include any If I remove the However, it also seems that these initial preprocessing attempts are done without Arduino.h added (looking at the preprocessed .cpp file), which means that if libraries are being selected based on stuff from Arduino.h, the preprocessing will not properly match the final compilation. |
I also had the "error" sometimes, that a warning appeared and after another warning was solves the other warning disappeared. Even though it was clearly not there. Might be so minor that it ignores it then? |
Indeed that first gcc call is |
I guess that adding the |
I need to include it in libraries as well which is a bug I think. I dont think that I need to include Arduino.h for register names. there must be something wrong here? The sketch above was just a simpler showcase, but the real (important) bug is inside libraries. |
@NicoHood, I'm not sure I understand your last comment? You're saying you must add |
Yes. Do I need to include Arduino.h just to get the USBCON definition? I remombered (might be wrong) To not need this, since its a normal reg and those register definitions should be passed via compiler? |
Register definitions for AVR are contained in #define USBCON _SFR_MEM8(0xD8)
#define VBUSTE 0
#define OTGPADE 4
#define FRZCLK 5
#define USBE 7
|
Right, thanks @matthijskooijman ! |
Thanks everybody. I guess the solution is to add platform includes ( |
This way it will be possible to get USBCON by including Arduino.h |
I now found the real cause of this problem I think. I recently remove the Arduino include in my project and though it was fixed: It was until I recompiled my own examples. What happens is:
This means we have a serious problem here with the preprocessor. To get away from the USB stuff imagine something like that. The error appears, but the code (with commented error) compiles fine. Even including Arduino.h does not help. There is something wrong, please reopen this issue. Otherwise I cannot use my library (and other libraries as well) correct. #if !defined(PIN)
#error
#endif
void setup() {
int t = PINB;
}
void loop() {} |
I don't think this is the right way to approach this. If your library needs Arduino.h, it should include it. It should not rely on being included from the .ino file or some other place that has previously included Arduino.h, since that is fragile, but also will not work for the library's own .cpp files (which will only include your own .h files, with the .ino file not being invovled). This might result in Arduino.h being included twice, but the include guards make sure it only takes effect the first time. In any case, the real issue here isn't fixed, so I'm reopening this issue. @ffissore now added the include path for Arduino.h so libraries can explicitly include it, but this doesn't work for the sketch. During normal compilation, it gets Arduino.h inserted automatically, but not during dependency-scanning, which means the depenency-scanning works in a different environment. When dependencies are selected based on defines from Arduino.h, this means that the results will be incorrect, but when This issue can occur more generically, when dependency selection, or As an example of the latter case, consider a library containing:
This is from a real-world library that is now failing, but just putting these lines a To show incorrect dependencies being selected, a slightly more complex example is needed. Consider two libraries, "Test" and "Foo": Test.h:
Test.cpp:
Foo.h:
Foo.cpp:
It is a bit of a contrived example, but the idea is that "Test" is a A sketch (or other library) wishing to support both older a newer
Now, if you compile this, you get:
What happens is that dependency scanning happens without Test.h in the To fix the errors during compilation, it would be sufficient to simply To fix both of these problems at the same time, the dependency-scanning edit: In addittion, the first dependency-scanning preprocessing run should already have the Arduino.h include added for sketches. Note that this one-by-one approach is exactly the approach proposed by Note that (AFAICS) the preprocessor-based approach also allows arduino-builder to bail out during |
Note that |
My project does not required Arduino.h. But the USBCON available check requires the definitions which are pulled in by arduino.h. And the compile error does not occur because my library cannot see USBCON, it occurs because the sketch cant see it. So normally I do not need to include Arduino.h from what I checked. I just want to clarify this. However its relevant and caused by the bug you mentioned, thats all fine. |
@NicoHood, I'll not respond to your last comment, since I think the details of how your library should ideally organize its includes would distract from the actual bug here (ping me on IRC if you want to know anyway). As you say, the actual bug you're seeing is that Arduino.h / USBCON is not available in the sketch during preprocessing, which is also illustrated by the |
@matthijskooijman I understand what's causing headaches here but unfortunately "preprocessing" = "gcc -M" and "gcc -E". I need a way to prevent gcc from aborting when preprocessing fails. I've found no "ignore-errors" flag, so the only that comes to mind right now is search&replacing |
Actually, |
Ah, there is a mismatch between what I thought happened and what actually happens. I have been assuming that you were running the preprocessor and scanning the output for missing include file error messages. I was also thinking that somehow an #error would take precedence, causing compilation to fail, while include errors would not. But looking more closely, it seems arduino-builder lets gcc do preprocessing and generate a dependency file, and scans that for missing include filenames. If an #error happens during this process, preprocessing fails and no output is generated. So, my proposal doesn't apply cleanly to this approach. You're saying you need a ignore errors flag, but even if you would have that, I'm not sure if the dependency-file based approach allows you to figure out which of the missing includes was first in the source file, which I still believe is essential to make this detection foolproof. So, how about switching to parsing gcc's output for include errors then? Or was this tried already? Looking over the gcc manpage for alternatives, I found the
This is from a file that first includes an empty foo.h and then a non-existent Test.h. |
If we want to parse the error output, |
This is old code that detected includes by letting gcc generate a dependency file and parsing that. It has been shown this method does not work (see arduino#33) and it was replaced by more robust methods since. Fixes arduino#108 Signed-off-by: Matthijs Kooijman <[email protected]>
Ref: #18
Used official Leonardo board
New issue, just try this sketch:
I have the same error within libraries unless I include Arduino.h. Not sure if this is intended, not sure if Arduino.h Includes the register definitions but I think this should be passed by default since things like DDRB should always be accessible without arduino layer.
However its weird that you can inlcude Arduino.h and it works then. And eve more weird that this does NOT help in a simple sketch.
The text was updated successfully, but these errors were encountered: