Skip to content

Preprocessing: include after prototype #35

Closed
Vincent14 opened this Issue Jul 12, 2012 · 6 comments

4 participants

@Vincent14

Hi !
I've found a bug in the last version of git. When you want include some libraries in an ino file with the official IDE, it will deplace all the includes in the source file at the top, before all function's prototype.

Take this simple source code for example :

#include <WaveHC.h>
#include <WaveUtil.h>
void setup() {
}
void loop() {
}
void play(FatReader &dir) {

}

The preprocessing of this file will build this file with the official IDE :

#include <WaveHC.h>
#include <WaveUtil.h>
#include "Arduino.h"
void setup();
void loop();
void play(FatReader &dir);
void setup() {
}
void loop() {
}
void play(FatReader &dir) {
}

But with ino, the result will be a little different :

#include <Arduino.h>
void setup();
void loop();
void play(FatReader &dir);
#line 1 "src/Rapport.ino"
#include <WaveHC.h>
#include <WaveUtil.h>
void setup() {
}
void loop() {
}
void play(FatReader &dir) {
}

So there is no error with the arduino's IDE, but with ino, the compiler say that FatReader is not defined.
Here is a quick patch to get all #include in the file and put it on top of the file. Note that my regex is certainly too simple but the principle is here.

diff --git a/ino/commands/preproc.py b/ino/commands/preproc.py
index 87539fa..5dfefe7 100644
--- a/ino/commands/preproc.py
+++ b/ino/commands/preproc.py
@@ -35,6 +35,7 @@ class Preprocess(Command):

         sketch = open(args.sketch, 'rt').read()

+        out.write('\n'.join(self.includes(sketch)))
         header = 'Arduino.h' if self.e.arduino_lib_version.major else 'WProgram.h'
         out.write('#include <%s>\n' % header)
         out.write('\n'.join(self.prototypes(sketch)))
@@ -47,6 +48,11 @@ class Preprocess(Command):
         matches = regex.findall(src)
         return [m + ';' for m in matches]

+    def includes(self, src):
+        regex = re.compile("\\s*#.*")
+        matches = regex.findall(src)
+        return [m.strip() for m in matches]
+
     def collapse_braces(self, src):
         """
         Remove the contents of all top-level curly brace pairs {}.
@nailxx
Амперка member
nailxx commented Jul 12, 2012

I've got the idea. Thanks for the bug hunting. It will be addressed.

@Vincent14

I've edit the presentation, it's more readable

@em
em commented Oct 19, 2012

Any update on this? Almost every project I've tried to build with ino instead of the IDE fails because of this. There's a lot of (frankly unnecessary) macros used in function definitions, and they wind up causing syntax errors because they haven't been included yet.

@nailxx
Амперка member
nailxx commented Oct 19, 2012

This will be fixed in next release. Unfortunattely there is too few spare time. Will try to find on next week. Thanks for the interest.

@mash
mash commented Jun 4, 2013

I took a different path against this issue, since @damienstuart 's didn't fix my problem.

@nailxx
Амперка member
nailxx commented Sep 23, 2013

Done in 0.3.6

@nailxx nailxx closed this Sep 23, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.