使用C ++

时间:2016-01-09 20:34:00

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

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

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

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

android.ndk {
    moduleName = "native"

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

对于我搜索的内容,每个人都有相同的功能。所以有人知道这是什么问题? 感谢

2 个答案:

答案 0 :(得分:0)

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

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)
     

确保添加&#39; LOCAL_CFLAGS&#39;添加&#39;包括$(CLEAR_VARS)&#39;

来自:How to set standard c99 for compile android NDK project

请查看上面的链接。

希望有所帮助

答案 1 :(得分:0)

问题解决了,一定是

CFlags.add("-std=c11")