How to use c++11 to program the Arduino?

前端 未结 6 1428
清酒与你
清酒与你 2020-12-04 17:43

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

6条回答
  •  無奈伤痛
    2020-12-04 18:16

    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.

    As a hack, you can use a small proxy program (should be cross-platform):

    //============================================================================
    // 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 
    
    #include 
    #include 
    #include 
    using namespace std;
    
    int main(int argc, char *argv[]) {
        vector arguments;
        vector 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);
    }
    
    1. Compile the program.
    2. Rename the original avr-g++.exe to avr-g++.orig.exe (or any other name).
    3. 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.

    You're done!

    Example avr-g++.ini:

    D:\Arduino\hardware\tools\avr\bin\avr-g++.orig.exe
    -std=c++0x
    

    Hope, that helps!

提交回复
热议问题