Skip to content
This repository has been archived by the owner on Aug 27, 2021. It is now read-only.

Preprocessing: include after prototype #35

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

Preprocessing: include after prototype #35

Vincent14 opened this issue Jul 12, 2012 · 6 comments

Comments

@Vincent14
Copy link

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 {}.
@nkrkv
Copy link
Member

nkrkv commented Jul 12, 2012

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

@Vincent14
Copy link
Author

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

@em
Copy link

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.

@nkrkv
Copy link
Member

nkrkv 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
Copy link

mash commented Jun 4, 2013

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

@nkrkv
Copy link
Member

nkrkv commented Sep 23, 2013

Done in 0.3.6

@nkrkv nkrkv closed this as completed Sep 23, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants