请教个c++语法问题(在android源代码中看到)

mmnnppqq 2012-12-26 09:25:06
是在android的JB代码的init_parser.c文件中:
#define KEYWORD(symbol, flags, nargs, func) \
[ K_##symbol ] = { #symbol, func, nargs + 1, flags, },

struct {
const char *name;
int (*func)(int nargs, char **args);
unsigned char nargs;
unsigned char flags;
} keyword_info[KEYWORD_COUNT] = {
[ K_UNKNOWN ] = { "unknown", 0, 0, 0 }, //这是什么语法?好像和lambda function有关?
#include "keywords.h"
};

其中,K_UNKNOWN是一个枚举类型,KEYWORD_COUNT是枚举类型中的最后一个变量。那些宏定义什么的都不是关键。

于是,我依样画葫芦的写了以下代码,可是用Android.mk编译出错了,,,
enum
{
AA,
BB,
COUNT
};

struct {
int m;
int count;
}aa[COUNT] = {
[ AA ] = { 2, 3 },
[ BB ] = { 5, 3 },
};


target C++: test <= main.cpp
main.cpp: In lambda function:
main.cpp:53:16: error: expected '{' before '=' token
main.cpp: In function 'int main(int, char**)':
main.cpp:53:16: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:53:25: error: no match for 'operator=' in '{(._18)0u} = {2, 3}'
main.cpp:53:25: note: candidate is:
main.cpp:53:14: note: main(int, char**)::<lambda()>& main(int, char**)::<lambda()>::operator=(const main(int, char**)::<lambda()>&)
main.cpp:53:14: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const main(int, char**)::<lambda()>&'
main.cpp: In lambda function:
main.cpp:54:16: error: expected '{' before '=' token
main.cpp: In function 'int main(int, char**)':
main.cpp:54:16: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
main.cpp:54:25: error: no match for 'operator=' in '{(._18)1u} = {5, 3}'
main.cpp:54:25: note: candidate is:
main.cpp:54:14: note: main(int, char**)::<lambda()>& main(int, char**)::<lambda()>::operator=(const main(int, char**)::<lambda()>&)
main.cpp:54:14: note: no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const main(int, char**)::<lambda()>&'
make: *** [test/main.o] Error 1
...全文
348 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmnnppqq 2012-12-26
  • 打赏
  • 举报
回复
谢谢,感觉这个文件没有什么特别的,我把其中的一些设置都抄过去了结果还是没有什么变化 # Copyright 2005 The Android Open Source Project LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ builtins.c \ init.c \ devices.c \ property_service.c \ util.c \ parser.c \ keychords.c \ signal_handler.c \ init_parser.c \ ueventd.c \ ueventd_parser.c ifeq ($(strip $(INIT_BOOTCHART)),true) LOCAL_SRC_FILES += bootchart.c LOCAL_CFLAGS += -DBOOTCHART=1 endif ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT))) LOCAL_CFLAGS += -DALLOW_LOCAL_PROP_OVERRIDE=1 endif LOCAL_MODULE:= init LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT) LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED) LOCAL_STATIC_LIBRARIES := libfs_mgr libcutils libc ifeq ($(HAVE_SELINUX),true) LOCAL_STATIC_LIBRARIES += libselinux LOCAL_C_INCLUDES += external/libselinux/include LOCAL_CFLAGS += -DHAVE_SELINUX endif include $(BUILD_EXECUTABLE) # Make a symlink from /sbin/ueventd to /init SYMLINKS := $(TARGET_ROOT_OUT)/sbin/ueventd $(SYMLINKS): INIT_BINARY := $(LOCAL_MODULE) $(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk @echo "Symlink: $@ -> ../$(INIT_BINARY)" @mkdir -p $(dir $@) @rm -rf $@ $(hide) ln -sf ../$(INIT_BINARY) $@ ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS) # We need this so that the installed files could be picked up based on the # local module name ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \ $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS) # make a link of modprobe to init SYMLINK_MODPROBE := $(TARGET_ROOT_OUT)/sbin/modprobe $(SYMLINK_MODPROBE): MODPROBE_BINARY := $(LOCAL_MODULE) $(SYMLINK_MODPROBE): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk @echo "Symlink: $@ -> ../$(MODPROBE_BINARY)" @mkdir -p $(dir $@) @rm -rf $@ $(hide) ln -sf ../$(MODPROBE_BINARY) $@ ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINK_MODPROBE)
ForestDB 2012-12-26
  • 打赏
  • 举报
回复
gcc的扩展。

# cat x.c
# include <stdio.h>

int main()
{
    struct {
        int a;
        int b;
    } foo[2] = {
        [1] = { .b = 2, .a = 1, },
        [0] = { 1, 2, },
    };

    return 0;
}

cc  -Wall   x.c   -o x

不需要什么flags
BadPattern 2012-12-26
  • 打赏
  • 举报
回复
可以参考一下源码/system/core/init目录下的Android.mk文件是怎么写的
mmnnppqq 2012-12-26
  • 打赏
  • 举报
回复
非常感谢,为了能够编译通过,我应该增加什么编译选项吗?我刚刚试了int a[6] = { [4] = 29, [2] = 15 };也是类似的编译错误。 回到我的初始问题,因为我是先编译了android(包括前面提到的那个源文件init_parser.c),说明其编译工具是支持这个特性的。然后我在这个里面新加了个文件编译却出错了,感觉应该是少了什么选项吧。 我试过增加-std=c++11,还是编译出错。 LOCAL_CFLAGS := -std=c++11
BadPattern 2012-12-26
  • 打赏
  • 举报
回复
标准C89需要初始化语句的元素以固定的顺序出现,和被初始化的数组或结构体中的元素顺序一样。 在ISO C99中,你可以按任何顺序给出这些元素,指明它们对应的数组的下标或结构体的成员名,并且GNU C也把这作为C89模式下的一个扩展。这个扩展没有在GNU C++中实现。 为了指定一个数组下标,在元素值的前面写上“[index] =”。比如: int a[6] = { [4] = 29, [2] = 15 }; 相当于: int a[6] = { 0, 0, 15, 0, 29, 0 };
BadPattern 2012-12-26
  • 打赏
  • 举报
回复
mmnnppqq 2012-12-26
  • 打赏
  • 举报
回复
嗯,看得不仔细,原来只是c被扩展了,没有包括c++。前面一看到gnu,就想着gcc包括了c和cpp支持了。。。
ForestDB 2012-12-26
  • 打赏
  • 举报
回复
gcc对C的扩展,明显C和CPP是两种语言吧,C++也没有这种语法吧。
mmnnppqq 2012-12-26
  • 打赏
  • 举报
回复
引用 5 楼 ForestDB 的回复:
gcc的扩展。 C/C++ code?12345678910111213141516171819# cat x.c# include <stdio.h> int main(){ struct { int a; int b; } foo[2] = { [1] = { .b = 2, .a = 1, }, ……
谢谢,我试了一下,如果是.c文件是可以的,如果改成cpp文件就不行了。 不知道这是为什么 aa@ubuntu:/tmp$ cc x.c aa@ubuntu:/tmp$ cp x.c x.cpp aa@ubuntu:/tmp$ cc x.cpp x.cpp: In function ‘int main()’: x.cpp:5:19: error: expected identifier before numeric constant x.cpp: In lambda function: x.cpp:5:22: error: expected ‘{’ before ‘=’ token x.cpp: In function ‘int main()’: x.cpp:5:22: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x x.cpp:5:24: error: no match for ‘operator=’ in ‘main()::<lambda()>() = 29’ x.cpp:5:29: error: expected identifier before numeric constant x.cpp: In lambda function: x.cpp:5:32: error: expected ‘{’ before ‘=’ token x.cpp: In function ‘int main()’: x.cpp:5:32: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x x.cpp:5:34: error: no match for ‘operator=’ in ‘main()::<lambda()>() = 15’

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧