Emscripten 中的 C++11 支持

C++11 support in Emscripten

我想使用 Emscripten 编译 C++ 代码,我在其中使用了一些 C++11 功能。不幸的是我得到一个错误:

index.cpp:13:18: error: expected expression
    vv.push_back({1.5f, 2.f});
                 ^
index.cpp:14:18: error: expected expression
    vv.push_back({5.f, 0});
                 ^
index.cpp:15:18: error: expected expression
    vv.push_back({1, 1});
                 ^
index.cpp:17:9: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
    for(auto& item : vv) {
        ^
index.cpp:17:20: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
    for(auto& item : vv) {

我不明白为什么会出现此错误。使用 emsdk.

激活最新的 Emscripten 和 Clang 版本

密码是:

#include<iostream>
#include<vector>

struct AA {
    float a;
    float b;
};

int main() {

    std::vector<AA> vv;

    vv.push_back({1.5f, 2.f});
    vv.push_back({5.f, 0});
    vv.push_back({1, 1});

    for(auto& item : vv) {

        std::cout << item.a << ' ' << item.b << std::endl;

    }

}

我什至收到一条消息:LLVM version appears incorrect (seeing "4.0", expected "3.7")

如果是真的,那应该是wotk,因为"Clang 3.3 and later implement all of the ISO C++ 2011 standard."

建议:将 -std=c++11 添加到您的编译器选项。

-Wc++11-extensions是添加警告的标志,不是添加C++11支持。