Android ndk 构建:为 C 和 C++ 指定警告级别

Android ndk build: Specifying warning level for both C and C++

我正在使用 ndk-build (r10e) 编译一个混合了 C 和 C++ 文件的库。我的 mk 文件有行:

LOCAL_CPPFLAGS += -Wall
LOCAL_CPPFLAGS += -Wno-unused-parameter
LOCAL_CFLAGS += -Wall
LOCAL_CFLAGS += -Wno-unused-parameter

然而,当我编译这段代码时:

void func2()
{
    unsigned int size = 3;
    int pos;
    for ( pos = 0; pos != size; ++pos )
    {

    }
}

在 cpp 文件中,我得到预期的警告:

file.cpp:4:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for ( int pos = 0; pos != size; ++pos )

在 c 文件中,我没有收到任何警告...

LOCAL_CFLAGS 不是为 C 文件指定警告级别的正确方法吗? 奖励问题:有没有办法使用简单变量为 CC++ 指定警告级别(以避免重复行 LOCAL_CPPFLAGS/LOCAL_CFLAGS)?

根据 GCC 文档的 3.8 Options to Request or Suppress Warnings 部分,-Wall 仅为 C++ 代码启用 -Wsign-compare。对于 C 代码,您需要使用 -Wextra,或者显式启用 -Wsign-compare

Bonus question: Is there a way to specify warning level for both C and C++

是的,LOCAL_CFLAGS 适用于 C 和 C++ 代码。 (source)