Arduino IDE的#ifdef bug
2015-10-09 12:56
337 查看
From:http://subethasoftware.com/2013/04/09/arduino-compiler-problem-with-ifdefs-solved/
In C, “#ifdef” or “#if defined()” are used to hide or include portions of code only if certain conditions are met. For example, my recent *ALL RAM* BBS experiment contains code for using the SD card library as well as the Ethernet library. I used #ifdef around
specific blocks of code so I could compile versions with or without either of those libraries. But all is not well in Arduino land. Consider this following, simple example:
?
This is supposed to only include the “byte mac[] =” line if “FOO” is defined, such as with:
?
However, on the current Arduino IDE (1.0.4), this simple code will fail with:
ifdef.ino: In function ‘void setup()’:
ifdef:18: error: ‘Serial’ was not declared in this scope
What? Suddenly “Serial.println()” won’t work? Moving the byte declaration outside of the #if def make it work. Very weird.
I also found a similar example, where I tried to comment out a function that used SD library variable types:
?
In this example, I did not want the fileReadln() function to be included unless I had defined FOO. But, compiling this produces:
ifdef:15: error: ‘File’ was not declared in this scope
ifdef:15: error: expected primary-expression before ‘char’
ifdef:15: error: expected primary-expression before ‘count’
ifdef:15: error: initializer expression list treated as compound expression
Unhelpful. And after wasting some time on this, I started a topic
in the Arduino.cc forums to ask if others were experiencing the same thing. And they were. A helpful post from parajew pointed
me to this site which helped explain the problem, and offered a workaround:
http://www.a-control.de/arduino-fehler/?lang=en
The pre-processor does some stuff behind the scenes, creating prototypes and including header files where needed, and it just does it wrong. The A-Control site figured out a simple workaround, which I trimmed a bit to just adding this at the top of my scripts:
?
…and now either of my examples will compile as intended. Thank you, parajew and A-Control! I can now move on to my next problem…
Arduino compiler problem with #ifdefs solved.
In C, “#ifdef” or “#if defined()” are used to hide or include portions of code only if certain conditions are met. For example, my recent *ALL RAM* BBS experiment contains code for using the SD card library as well as the Ethernet library. I used #ifdef aroundspecific blocks of code so I could compile versions with or without either of those libraries. But all is not well in Arduino land. Consider this following, simple example:
?
?
ifdef.ino: In function ‘void setup()’:
ifdef:18: error: ‘Serial’ was not declared in this scope
What? Suddenly “Serial.println()” won’t work? Moving the byte declaration outside of the #if def make it work. Very weird.
I also found a similar example, where I tried to comment out a function that used SD library variable types:
?
ifdef:15: error: ‘File’ was not declared in this scope
ifdef:15: error: expected primary-expression before ‘char’
ifdef:15: error: expected primary-expression before ‘count’
ifdef:15: error: initializer expression list treated as compound expression
Unhelpful. And after wasting some time on this, I started a topic
in the Arduino.cc forums to ask if others were experiencing the same thing. And they were. A helpful post from parajew pointed
me to this site which helped explain the problem, and offered a workaround:
http://www.a-control.de/arduino-fehler/?lang=en
The pre-processor does some stuff behind the scenes, creating prototypes and including header files where needed, and it just does it wrong. The A-Control site figured out a simple workaround, which I trimmed a bit to just adding this at the top of my scripts:
?
相关文章推荐
- 第二十九篇:使用SOUI的SMCListView控件
- [Android UI开发] viewpager fragment 切换时oncreateview调用多次问题
- iOS UILabel显示HTML文本
- 解决Django 提交表单时403错误:CSRF verification failed. Request
- UIView的layoutSubviews和drawRect方法何时调用
- 探究UIImagePickerController
- 循环队列_queue
- java中List、Map、Set、Collection、Stack、Queue等的使用
- Fourier Transform Intro - Oscillation frequency vs Angular frequency Expression
- poj 3498 March of the Penguins(最大流+拆点)
- Leetcode89: Unique Paths
- StringBuilder、StringBuffer和String三者的联系和区别
- UISegmentedControl的基本用法
- iOS开发UI篇—UITabBarController简单介绍
- uitableview顶部多出20距离, UIScollView顶部多出64距离
- [Leetcode] Longest Consecutive Sequence
- request和response
- 使用Sequel Pro通过ssh远程连接MySQL出错:The SSH Tunnel has unexpectedly closed
- Jenkins系列之—17 一个完整的JENKINS下的ANT BUILD.XML文件
- Xcode Build Search Paths设置