如何使用 c++11 对 Arduino 进行编程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16224746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use c++11 to program the Arduino?
提问by walrii
How can I use c++11
when programming the Arduino? I would be fine using either the Arduino IDE or another environment. I am most interested in the core language improvements, not things that require standard library changes.
c++11
Arduino 编程时如何使用?我可以使用 Arduino IDE 或其他环境。我最感兴趣的是核心语言的改进,而不是需要更改标准库的东西。
采纳答案by jose.angel.jimenez
As of version 1.6.6, the Arduino IDE enables c++11 by default.
从版本 1.6.6 开始,Arduino IDE 默认启用 c++11。
For older versions, read on:
对于旧版本,请继续阅读:
It is very easy to change the flagsfor any element of the toolchain, including the assembler, compiler, linker or archiver.
更改工具链中任何元素的标志非常容易,包括汇编器、编译器、链接器或归档器。
Tested on the Arduino IDE version 1.5.7 (released on July 2014),
在 Arduino IDE 1.5.7 版(2014 年 7 月发布)上测试,
- Locate the platform.txtfile,
- AVR architecture => {install path}\hardware\arduino\avr\platform.txt
- SAM architecture => {install path}\hardware\arduino\sam\platform.txt
- Inside that file, you can change any flag, for instance,
- compiler.c.flags for changing the default compilation flags for C++ files.
- compiler.cpp.flags for changing the default compilation flags for C++ files.
- You can also change any of the "recipes" or compile patters, at the corresponding section of the configuration file, under the title "AVR/SAM compile patterns".
- After making the changes, you must restart the Arduino IDE, at least on version 1.5.7.
- 找到platform.txt文件,
- AVR 架构 => {安装路径}\hardware\arduino\avr\platform.txt
- SAM 架构 => {安装路径}\hardware\arduino\sam\platform.txt
- 在该文件中,您可以更改任何标志,例如,
- compiler.c.flags 用于更改 C++ 文件的默认编译标志。
- compiler.cpp.flags 用于更改 C++ 文件的默认编译标志。
- 您还可以在配置文件的相应部分,标题“AVR/SAM 编译模式”下更改任何“配方”或编译模式。
- 进行更改后,您必须重新启动 Arduino IDE,至少在 1.5.7 版上。
For instance,
例如,
To enable support for C++11 (C++0x), tested on Arduino IDE versions 1.5.7 and 1.5.8, you will simply add the flag "-std=gnu++11" at the end of the line starting with compiler.cpp.flags=".
要启用对 C++11 (C++0x) 的支持,在 Arduino IDE 版本 1.5.7 和 1.5.8 上测试,您只需在行尾添加标志“-std=gnu++11”使用 compiler.cpp.flags="。
It is expected that C++11 is enabled by default in the near futureon the Arduino IDE. However, as of version 1.5.8 (Oct 2014) it is still not the case.
预计不久的将来,Arduino IDE 将默认启用 C++11 。但是,从 1.5.8 版(2014 年 10 月)开始,情况仍然不是这样。
回答by Capt
Arduino IDE 1.6.6 and newer have C++11 enabled by default (they have the compiler flag "-std=gnu++11" set in the platform.txt file).
Arduino IDE 1.6.6 和更新版本默认启用 C++11(它们在 platform.txt 文件中设置了编译器标志“-std=gnu++11”)。
回答by asheeshr
Firstly, only gcc
4.7 and above (and therefore avr-gcc
4.7 and above) support C++11
. So, check the versions installed with :
首先,只有gcc
4.7 及更高版本(因此avr-gcc
4.7 及更高版本)支持C++11
. 因此,请检查安装的版本:
gcc --version
avr-gcc --version
If avr-gcc
is 4.7 or higher, then you may be able to use C++11
.
如果avr-gcc
是 4.7 或更高版本,那么您也许可以使用C++11
.
The Arduino IDE does not support custom compiler flags. This has been requestedbut has not yet been implemented.
Arduino IDE 不支持自定义编译器标志。这已被要求但尚未实施。
So, you are left with having to use other environments or to compile your program directly from the command line.
因此,您不得不使用其他环境或直接从命令行编译您的程序。
In case, of compiling directly from the command line using avr-gcc
, you simply need to add an extra compiler flag for enabling C++11 support.
如果使用 直接从命令行编译avr-gcc
,您只需添加一个额外的编译器标志以启用 C++11 支持。
-std=c++11
For specific development environments, most would support editing of the compiler flags from the build options within the IDE. The above mentioned flag needs to be added to the list of flags for each environment.
对于特定的开发环境,大多数都支持从 IDE 中的构建选项编辑编译器标志。需要将上述标志添加到每个环境的标志列表中。
C++0x
was the name of working draft of the C++11
standard. C++0x
support is available gcc
4.3 onwards. However, this is strictly experimental support so you cannot realiably expect C++11
features to be present. Hereis the complete list of features available with the corresponding version of gcc
. The availability of features in avr-gcc
will be the same as whats available in the corresponding gcc
version.
C++0x
是C++11
标准工作草案的名称。C++0x
从gcc
4.3 开始支持。但是,这是严格的实验支持,因此您不能真正期望C++11
功能存在。以下是相应版本的可用功能的完整列表gcc
。功能的可用性avr-gcc
将与相应gcc
版本中的可用功能相同。
The compiler flag for C++0x
is :
的编译器标志C++0x
是:
-std=c++0x
回答by fAX
Please, note, that there is no easy way to specify additional flags from Arduino IDE or use other IDE (Eclipse, Code Blocks, etc) or command line.
请注意,没有简单的方法可以从 Arduino IDE 指定其他标志或使用其他 IDE(Eclipse、代码块等)或命令行。
As a hack, you can use a small proxy program (should be cross-platform):
作为一个hack,你可以使用一个小的代理程序(应该是跨平台的):
//============================================================================
// Name : gcc-proxy.cpp
// Copyright : Use as you want
// Description : Based on http://stackoverflow.com/questions/5846934/how-to-pass-a-vector-to-execvp
//============================================================================
#include <unistd.h>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
vector<string> arguments;
vector<const char*> aptrs;
// Additional options, one per line
ifstream cfg((string(argv[0]) + ".ini").c_str());
if (cfg.bad())
cerr << "Could not open ini file (you're using proxy for some reason, er?)" << endl;
string arg;
while (cfg) {
getline(cfg, arg);
if(arg == "\r" || arg == "\n")
continue;
arguments.push_back(arg);
}
for (const string& arg : arguments)
aptrs.push_back(arg.c_str());
for (int i = 1; i < argc; ++i)
aptrs.push_back(argv[i]);
// Add null pointer at the end, execvp expects NULL as last element
aptrs.push_back(nullptr);
// pass the vector's internal array to execvp
const char **command = &aptrs[0];
return execvp(command[0], command);
}
- Compile the program.
- Rename the original avr-g++.exe to avr-g++.orig.exe (or any other name).
- Create avr-g++.ini file where the first line is FULL path to the original program (e.g. D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe) and add additional parameters, one per line, as desired.
- 编译程序。
- 将原始 avr-g++.exe 重命名为 avr-g++.orig.exe(或任何其他名称)。
- 创建 avr-g++.ini 文件,其中第一行是原始程序的完整路径(例如 D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe)并添加其他参数,每行一个,如预期的。
You're done!
你完成了!
Example avr-g++.ini:
示例 avr-g++.ini:
D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe
-std=c++0x
Hope, that helps!
希望,这有帮助!
回答by hoosierEE
I use Inoand this worked:
我使用Ino并且这有效:
ino build -cppflags="-std=c++0x"
ino build -cppflags="-std=c++0x"
This generated a hex file at least 15k in size (that's with optimizations turned on), compared to about 5k for the standard build, which is a consideration for a poor little Atmega328. Might be okay for one of the microcontrollers with a lot more program space.
这生成了一个大小至少为 15k 的十六进制文件(即打开了优化),而标准构建的大小约为 5k,这是一个可怜的小 Atmega328 的考虑因素。对于具有更多程序空间的微控制器之一可能没问题。