【问题标题】:Loop initial declarations are only allowed in C99 or C11 mode循环初始声明只允许在 C99 或 C11 模式下
【发布时间】:2016-01-09 20:33:59
【问题描述】:

我在使用 c++ 文件构建我的 android 项目时遇到问题。它说我必须用 C++11 编译:

error: 'for' loop initial declarations are only allowed in C99 or C11 mode

我知道这意味着什么,但我想使用 c++11。我将它包含在 gradle config 中:

android.ndk {
    moduleName = "native"

    stl = "gnustl_static"
    cppFlags += "-std=c++11"
    cppFlags += "-fexceptions"
    ldLibs.addAll(['android', 'log', 'OpenSLES'])
}

对于我搜索的内容,每个人都有相同的内容并且有效。有人知道是什么问题吗?

【问题讨论】:

    标签: android c++ c++11 android-ndk


    【解决方案1】:

    我想你已经有一个这样的循环:

    for(int i = low; i <= high; ++i)
            {
                    res = runalg(i);
                    if (res > highestres)
                    {
                            highestres = res;
                    }
    
            }
    

    请在循环外声明 var i

    应该能解决问题。

    如果可能,还要考虑使用while 而不是for

    编辑:我找到了一个您可能会感兴趣的解决方案:

    在你的 Android.mk 中添加

    LOCAL_CFLAGS += -std=c99
    

    例如:

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_CFLAGS += -std=c99
    LOCAL_SRC_FILES := com_example_ndktest_TestLib.c
    LOCAL_MODULE := com_example_ndktest_TestLib
    include $(BUILD_SHARED_LIBRARY)
    

    确保在添加“include $(CLEAR_VARS)”后添加“LOCAL_CFLAGS”

    发件人:How to set standard c99 for compile android NDK project

    请检查上面的链接。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      问题解决了,一定是

      CFlags.add("-std=c11")
      

      【讨论】:

        猜你喜欢
        • 2017-10-28
        • 2015-06-02
        • 2021-03-29
        • 2015-07-21
        • 2021-06-29
        • 2011-08-18
        • 2013-01-19
        • 2010-09-06
        相关资源
        最近更新 更多