expected declaration specifiers or '...' before numeric报错

expected declaration specifiers or '...' before numeric constant malloc.c C/C++ Problem

代码是这样写的

__align(32) u8 mem1base[MEM1_MAX_SIZE];                                                    //内部SRAM内存池
__align(32) u8 mem2base[MEM2_MAX_SIZE] __attribute__((at(0X68000000)));                    //外部SRAM内存池

//内存管理表
u16 mem1mapbase[MEM1_ALLOC_TABLE_SIZE];                                                    //内部SRAM内存池MAP
u16 mem2mapbase[MEM2_ALLOC_TABLE_SIZE] __attribute__((at(0X68000000+MEM2_MAX_SIZE)));    //外部SRAM内存池MAP

下面还有一句调用mem1base和mem2base的语句

//内存管理控制器
struct _m_mallco_dev mallco_dev=
{
    my_mem_init,                //内存初始化
    my_mem_perused,                //内存使用率
    mem1base,mem2base,            //内存池
    mem1mapbase,mem2mapbase,    //内存管理状态表
    0,0,                           //内存管理未就绪
};

编译结果报错:

'mem1base' undeclared here (not in a function); did you mean 'mem1mapbase'?
'mem2base' undeclared here (not in a function); did you mean 'mem2mapbase'?

expected declaration specifiers or '...' before numeric constant
expected declaration specifiers or '...' before numeric constant

请问是什么原因导致了这样呢?

看看是不是函数声明没有写或者一些符号,特别是上一行的分号遗漏

参考GPT和自己的思路:从您提供的代码来看,可能出现这些报错的原因是因为在使用mem1base和mem2base之前没有在代码中声明它们。建议您在使用之前添加如下声明:

extern u8 mem1base[]; //或者指定具体大小: extern u8 mem1base[MEM1_MAX_SIZE];
extern u8 mem2base[]; //或者指定具体大小: extern u8 mem2base[MEM2_MAX_SIZE];

这样就可以让编译器知道这两个变量的存在并且正确分配它们所需要的内存空间了。同时,对于第二个报错"expected declaration specifiers or '...' before numeric constant",可能是因为在代码中使用数字常量时出现了语法错误,您可以检查一下代码中是否有类似错误的部分。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^