I have a 1,100 line sketch broken into 6 files in a single directory. I found an issue whereby one of my files requires at least 18 line breaks (blank or filled) before the first function or else I get a compile error. Oddly, if I group all code into a single sketch those 18 line breaks are not necessary. Reordering my code does not seem to be (nor fix) the issue. My question: Is this a known issue?
Using:
Mac 10.12.4
Arduino 1.8.4
RedBear Duo board
Screenshots show the IDE successfully compiling with the 18 added line breaks and failing with only 17. Another screenshot shows it all compiling fine when grouped into a single sketch without the added lines.
Your images are unreadable. Don't post pictures of text. Just copy and paste the text.
Don't you think people might need to see the code that is causing the problem? You don't even say if they are .ino files or .h or .cpp files or whatever.
On the other hand, I can't see myself reading 1100 lines of code or 6 files.
When did you first discover this problem? I doubt if you wrote all 1100 lines of code without testing any of it. Can you replicate the problem with a short program?
My very wild guess is that there is one or more strange characters (maybe some unicode has crept in) in one of your files.
They are .ino files. I agree that no one will want to read so much code.
The sketch is several months old and has been compiled successfully 100's of times. The problem was found yesterday when I removed a function from this particular .ino file. At first, I commented the removed function out and everything was fine. Then I deleted the function altogether and it failed to compile. After trying a number of other things I removed the function line by line to find where the error was introduced. It was that that led me to realize it was the lines themselves and not the code that was the issue.
Good suggestion on the "strange character". I will try cleaning it and report what happens.
It sounds to me like the problem is caused by the automatically generated function prototypes being put in the wrong place. The way you can check this is by looking at the generated .ino.cpp file in the sketch subfolder of the temporary build folder. You can find the temporary build folder location by turning on verbose output during compilation in File > Preferences, compiling, and then examining the contents of the black console window at the bottom of the Arduino IDE window.
Usually the workaround for this is to manually create the function prototypes for the functions that are causing the failure. The sketch preprocessor will not do automatic function prototype generation for any function that already has a prototype.
Generally the Arduino developers are interested in fixing these sorts of failures whenever possible if the specific cause can be determined and a complete minimal demonstration code provided. I have seen a couple issues reported where they've just said "we're not going to be able to fix this, users will just need to manually declare function prototypes in this particular case". There is a big change going on right now where they have completely reworked the sketch preprocessor so it's possible this issue was already solved. You can check this by testing the code that doesn't compile on Arduino IDE 1.8.4 with the latest Arduino IDE 1.9 beta build, which you can download here:
The IDE combines all the ino files into a cpp file. You can turn verbose mode on when compiling, and locate, and examine, the cpp file. Perhaps a clue-by-four will present itself when you do that.
So, when you compile, you "get a compile error". WHAT error?? The error message gives the exact line number, and character position where the invalid syntax is located, and a detailed explanation of WHAT the error is. But, you didn't bother to GIVE us that complete, detailed error message. It's like calling a mechanic and telling him the "Check Engine" light on your car is on, and asking him to tell you what's wrong with the engine....
Thank you for the replies. I will check the cpp file as you suggest.
I also tore down the code to a handful of lines - the minimum necessary to reproduce the issue. It is still 6 .ino files, but only a few lines of code in each file. The code is, of course, nonsense because the surrounding context is gone, but it will allow anyone who is interested to reproduce the issue I see in the full set of files.
(I have validated this with another developer, so you should see it too.)
The steps:
1: Open this sketch in the Arduino IDE, compile, it will be fine
2: On the sample_01_Utilities tab delete one of the rows between row 1 and 19
3: Compile again, you should see an error for tab sample_02_BLE that says "'handle_ota_data' was not declared in this scope"
(But of course, you haven't change code to cause the compile error.)
4: Now go to the 1st tab ("sample") and delete a row between 1 and 19 and compile again. This time the issue above does not occur.
There is some relationship between the block comment and number of rows on the first tab and the rows on the 3rd tab. It cannot be reproduced when using "//" to comment out a line on the first tab.
I am sure the community would prefer me to add code snippets, but since it must be reproduced with multiple .ino in a single directory I have uploaded a zip file.
As I suspected, the problem is that the sketch preprocessor is adding the function prototypes inside the block comment. This also occurs with Arduino IDE 1.8.5. Here's the generated .cpp file:
#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample.ino"
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample.ino"
/*
Shorten this commented section by a row (any row between 1 and 19) and the issue does not appear
#line 19 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_01_Utilities.ino"
void function1();
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_02_BLE.ino"
int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t size);
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_03_OTA.ino"
static int handle_ota_data(uint8_t *buffer, uint16_t size);
#line 3 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_04_Main.ino"
void setup();
#line 6 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_04_Main.ino"
void loop();
#line 19 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_01_Utilities.ino"
*/
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_00_Variables.ino"
int x;
int y;
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_01_Utilities.ino"
// Line 11
// Line 13
void function1() {
}
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_02_BLE.ino"
int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t size)
{
if(x == y)
{
} else if (x == 0){
handle_ota_data(buffer, size);
}
return 0;
}
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_03_OTA.ino"
static int handle_ota_data(uint8_t *buffer, uint16_t size){
}
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_571986\\sample_04_Main.ino"
void setup(){
}
void loop(){
}
I'm happy to report that this problem does not occur with Arduino IDE 1.9 Beta so there is no need to report the bug to the developers.
You can fix the issue by manually adding the problematic function prototypes:
void function1();
int gattWriteCallback(uint16_t value_handle, uint8_t *buffer, uint16_t size);
static int handle_ota_data(uint8_t *buffer, uint16_t size);
void setup();
void loop();
This is great, thank you for your insight and expertise. Much appreciated. For other user's sake, I will confirm on this thread once I have validated the fix on my end (although I think it is clear from your analysis that it is solved.)
Hi all,
I know this is a very old thread, but I'm afraid this issue is still not solved. I'm running into a very similar problem while using Arduino IDE version 1.8.8. Trying version 1.9.0-beta indeed solves this problem, but has multiple other problems (can't program or write data to ESP SPIFFS while serial monitor is running & sometimes all menu items are shown twice). I hope this problem will be solved in a normal release soon.
Thank you,
woutput